2

What I want is to search for a unique id (not the primary key) in a table where 99% of my searches for this unique id will be in the latest added rows.

So I want that my search begins from the last row to the first row. as the column is unique, when the search will start from the last row, the value will be found too much faster.

so the question is how can I obligate mysql to start searching in my procedure from the last row??

I found this question similar, or have same idea like my question but I didn't found any working answer.

any help is appreciated :))

Community
  • 1
  • 1
Chris Sim
  • 4,054
  • 4
  • 29
  • 36

3 Answers3

0

If there is a date or ordered number associated with the time it was added and you only want to return a set # of rows then adding an index to that column and ORDER BY it DESC.

SELECT [COLUMNS] FROM [TABLE] WHERE [CRITERIA] ORDER BY [INDEX] DESC LIMIT [NUMBER]

I believe that will result in something similar to what you're looking for. Also adding indexes to the columns in your search criteria can speed up your query and help avoid FULL TABLE Scans.

NSjonas
  • 10,693
  • 9
  • 66
  • 92
0

what about something like this...only a guess

$query = "select searchkey from tablename where searchkey like %$searchtext% order by id desc";
Deepu Sasidharan
  • 5,193
  • 10
  • 40
  • 97
0
SELECT DISTINCT (COLUMNNAME) FROM TBLNAME WHERE FIELDNAME LIKE %YOURVALUE% ORDER BY ID DESC
SagarPPanchal
  • 9,839
  • 6
  • 34
  • 62