I'm using mysql connector to connect with TiDB. I have found that indexes are not getting used in my queries. After my analysis i have found that due to casting indexes were not used. E.g
CREATE TABLE Employee (
ID BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
EmpID VARCHAR(200) NOT NULL
);
CREATE INDEX idx_EmpID ON Employee(EmpID);
1.Explain SELECT * FROM Employee WHERE EmpID= 123;
2.Explain SELECT * FROM Employee WHERE EmpID= '123';
In above example if i run first query and check the execution plan using explain then no indexes are used however in second query as I'm providing quotes for varchar datatype index is getting used.
However this solution needs to be tested via .net mysqlconnector library. Currently I'm using mysql command parameters to execute queries.
1.Current Logic with AddWithValue function
MySqlCommand cmd1 = new MySqlCommand
{
CommandText = "Select * from Employee where EmpID = @EmpID"
};
cmd1.Parameters.AddWithValue($"@EmpID", 1234);
2.Updated logic by providing datatype for the field using Add function
MySqlCommand cmd1 = new MySqlCommand
{
CommandText = "Select * from Employee where EmpID = @EmpID"
};
cmd1.Parameters.Add($"@EmpID", MySqlDbType.VarChar).Value = 1234;
I want to know the query representation at server side when processing the request. Would the updated logic .i.e point 2 would form the query by providing quotes for varchar datatype?