For a table with 600000 data, how to optimize the use of WHERE + ORDER BY SQL?

Boss, the following sentence SQL, the execution time has reached 6s, what should I do?

in the end, I chose the scheme of "create a separate index" with the least impact.
not sure this is the best result! just because of my environment, comprehensive testing is more difficult, so I chose the "fastest, least impact" approach.
thanks again to @ Yujiaao and @ chenbinhua for their help.

Business scenario

sort by code > click_count , and get the first 20 results with c_id as 17 + choice as 1 .

SQL original sentence

SELECT `id` 
FROM `akb48` 
WHERE `c_id` = 17 AND `choice` = 1 ORDER BY `click_count` DESC LIMIT 20 OFFSET 600;

SQL analysis

"1"SIMPLE"akb48" N "index"IDX_C_ID,IDX_C_ID_POINT"IDX_CLICK_COUNT"4" N "10281"0.61"Using where"

select_type: SIMPLE;
type: index;
key: IDX_CLICK_COUNT
key_len: 4
rows: 10281;
filtered: 0.61%;

add:
table data 600,000 items , visual observation is because ORDER BY takes the lead in execution? Causes other indexes to be unavailable.

Jun.09,2022

do you need to jointly index the c_id, choice, and click_count fields


'choice' field?


wouldn't it be better to filter out a collection first, and then sort the from subquery?
I do not have an index here, and similar queries will not exceed 1 second

Menu