Answer the question
In order to leave comments, you need to log in
How to remove the glitch of calculating the ordinal number in a MYSQL table?
Hello. Faced a mega nasty glitch. The task is to find and calculate the sequence number of a record with a certain ID in a table with more than 500,000 records. I make a request:
SELECT rownum, id FROM (
SELECT @rownum := @rownum +1 AS rownum, companies.id
FROM companies, (SELECT @rownum :=0) r
WHERE `companies`.`delete_date` IS NULL
ORDER BY companies.mng_status DESC
LIMIT 5000
) AS src
WHERE src.id =116912
Answer the question
In order to leave comments, you need to log in
Good afternoon,
what is the value in the companies.mng_status field that is sorted by?
Let's assume that the top 5000 always returns the first subtracted data for the index with the value mng_status = 1.
in the case when we request all the data or 15000, the skul decides to take not from the index, but to scan the table.
then with the status mng_status = 1 there will be much more data and the specific id in it will not necessarily be in the same place as it was when subtracting from the index, the sorting in the index itself may be different.
you can try adding a hint to use the index and see what happens: USE INDEX (col_index)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question