I currently have this table (myTable) in my database:
user start
----------------------------
Adam 12345
Alex 123
Benny 2345
In my program, I accept a string from user, eg: 12345678
My objective is to select out the row where user input starts with myTable.Start
-- For example, it would be great to have something like:
select * from myTable where "12345678".startsWith(start)
-- and returns me Adam, 12345 & Alex, 123
As of now I'm using
select user, start
from myTable where charindex(start, "12345678") = 1
order by start desc
which does the job, but in absolute terrible performance, myTable row count is about near a million, I'm not sure if indexing start would help as I'm not doing a direct compare in this case.
Does anyone know a better way to accomplish this?
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `myTable` ADD INDEX `mytable_idx_start` (`start`);
SELECT
myTable.user,
myTable.start
FROM
myTable
WHERE
charindex(myTable.start, myTable."12345678") = 1
ORDER BY
myTable.start DESC