【发布时间】:2015-05-14 07:23:40
【问题描述】:
我正在处理一组相当大的数据,并试图从四个不同数据的每个组合中创建一个查询。所有这些部分组合起来形成了惊人的 122,000,000 行。然后,我试图找到一个小于某个数量的权重,并按另一个值从高到低排序。
我可以使用weight < x 没问题。
我可以使用weight < x order by height ASC 没问题。
当 x 位于上端和下端附近时,我什至可以使用weight < x order by height DESC。但是一旦它开始爬到中间,它很快就会从几秒钟上升到几分钟,然后到“我不会等那么久”。
有什么想法吗? (名称已更改,但类型未更改)
创造:
CREATE TABLE combinations (
id bigint(20) unsigned NOT NULL auto_increment,
up smallint(2) NOT NULL,
left smallint(2) NOT NULL,
right smallint(2) NOT NULL,
down smallint(2) NOT NULL,
weight decimal(5,1) NOT NULL,
width smallint(3) NOT NULL,
forward decimal(6,2) NOT NULL,
backwards decimal(5,2) NOT NULL,
in decimal(7,2) NOT NULL,
out smallint(3) NOT NULL,
height smallint(3) NOT NULL,
diameter decimal(7,2) NOT NULL,
PRIMARY KEY (id)
);
索引
ALTER TABLE combinations ADD INDEX weight_and_height(weight,height);
查询
SELECT * FROM combinations WHERE weight < 20 ORDER BY height DESC limit 0,5;
解释
| id | select type | table | type | possible_keys | key | key_len | ref | rows | extra |
| 1 | simple | combinations | index | weight_and_height | weight_and_height | 5 | NULL | 10 | using where |
【问题讨论】:
标签: mysql indexing sql-order-by where where-clause