I am using mysql database which has only one table "data" with 17,151257 rows.This table has a column string. i want to print all the rows in which string column contains a particular query string (stored in "entered_query" variable), so i used following:
SELECT DISTINCT * from data WHERE string LIKE '%".$entered_query."%' limit 10
As obvious above query is taking too much time to execute.
I have read that indexing can be used but how in this case ?
I have also thought of dividing whole data into 10 different rows and then executing 10 parallel queries using perl DBI.
Now I have following questions :
- How to reduce the execution time ?
- I have heard that "LIKE" in mysql query avoids the indexing so is there any better alternative for above query ?
- When we use limit 10 in mysql query then does mysql stops executing as soon as it finds first 10 results or first it searches whole data for given query then it returns first 10 results
$entered_query
doesn't contain quite what its name suggest. Otherwise that might be a way to speed up your queries that you wouldn't want. – DishwaterDISTINCT *
supposed to do and is there a lower limit to the size of your entered string? – Dishwater$entered_query
contains exactly what the user entered, you have a sql-injection problem. 2. If your table has a primary key (like id), theDISTINCT
is unnecessary, yet expensive. 3. If there is a lower limit to the length of the search terms your users can enter, you might have a chance to store your data differently without drowning in combinatorial explosion: you might consider storing each possible substring. – Dishwater