我有 MySQL 数据库:
CREATE TABLE IF NOT EXISTS `tableexample` (
`id` int(11) NOT NULL auto_increment,
`val0` tinyint(1) NOT NULL,
`val1` tinyint(1) NOT NULL,
`val2` tinyint(1) NOT NULL,
`val3` tinyint(1) NOT NULL,
`val4` tinyint(1) NOT NULL,
`val5` tinyint(1) NOT NULL,
.................
`val1996` tinyint(1) NOT NULL,
`val1997` tinyint(1) NOT NULL,
`val1998` tinyint(1) NOT NULL,
`val1999` tinyint(1) NOT NULL,
UNIQUE KEY `id` (`id`),
) ENGINE=MEMORY ;
这个查询最多可以有 500 个字段:
SELECT
( ABS(`val1`-12)
+ ABS(`val22`-22)
+ ABS(`val23`-3)
+ ABS(`val45`-87)
+ ABS(`val76`-23)
........
+ ABS(`val1324`-7)
+ ABS(`val1576`-29)
) as valcal,
id
FROM `tableexample`
ORDER BY valcal ASC
LIMIT 5
此外,ABS
对于每个查询(12、22、3、87、23、7、29,...),来自的数字都是唯一的,并且可以在 0-99 范围内。
我做了一些测试:
for 1000 records the sql run in 0.024 seconds
for 10000 records the sql run in 0.231 seconds
for 50000 records the sql run in 1.123 seconds
for 100000 records the sql run in 2.179 seconds
for 200000 records the sql run in 4.316 seconds
for 300000 records the sql run in 6.451 seconds
你有什么优化技巧吗,因为数据库中有大约 300,000 条记录?