简体   繁体   中英

MySQL InnoDB Slow - Order By Case When

I have a table with a field flag with a value of 0, 1 or 2. I need to order the results with a flag value of 2 first followed by the remaining results.

At the moment I am using SELECT * FROM table ORDER BY CASE WHEN flag = 2 THEN 1 ELSE 0 and while this worked fine (<0.1s) on MyISAM, this appears very slow (9s for 10k rows) on InnoDB (something we are moving over to).

I've tried ORDER BY FIELD (flag, '2') DESC with similar results.

flag has an index and works very quick if I do a simple ORDER BY flag DESC , but this gives priority to those with flag 1 over those with flag 0, something I don't want.

Is there any tweaking of InnoDB that could help, or is there a more efficient way of ordering these results?

你试过了吗:

SELECT * FROM table ORDER BY (flag = 2) DESC

Thanks for the thoughts on alternative approaches to the ordering problem.

However, the issue lies with the InnoDB settings of my.ini :

# 60-80 % of available RAM.
innodb_buffer_pool_size = 1024M
innodb_additional_mem_pool_size = 128M

# 25 % of buffer pool size.
innodb_log_file_size = 256M
innodb_log_buffer_size = 128M

It appears that using InnoDB requires some configuring of my.cnf ; I am used to MyISAM that requires very little.

Obviously there's plenty of reading to do, including the links below, but hopefully this will help others get started as it has myself:

MySQL Manual

One of Many Third Party Articles

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM