我有一张 table 。 规范[id,name,store_id,section_id,category_id,user_id,model_id]
我的查询很简单:
SELECT * FROM specs
WHERE
store_id=1
AND
section_id=1
AND
category_id=1
AND
user_id=1
AND
model_id=1
我收到的结果是规范列表。
如果我将查询更改为:
SELECT * FROM specs WHERE model_id=1
我会得到相同的结果,因为该表可以有重复项(当然不包括 id
)
数据样本(注意:
id | name | model_id | section_id |category_id | user_id
------------------------------------------------------------------
1 | me | 22 | 5 | 11 | 777
2 | you | 22 | 5 | 11 | 777
3 | me | 22 | 5 | 11 | 777
4 | me | 22 | 5 | 11 | 777
5 | you | 33 | 5 | 11 | 777
6 | they | 33 | 5 | 11 | 777
7 | them | 33 | 5 | 11 | 777
8 | we | 33 | 5 | 11 | 777
9 | we | 33 | 5 | 11 | 777
10 | them | 44 | 5 | 11 | 776
11 | they | 44 | 5 | 11 | 776
12 | me | 44 | 5 | 11 | 776
13 | you | 44 | 5 | 11 | 776
14 | they | 44 | 5 | 11 | 776
15 | them | 44 | 5 | 11 | 776
数据库是MySQL。
问题:由于表将包含 500k-1000k 条记录,使用第一个查询还是第二个查询会更快?更多的条件会使它更快?慢一点?
编辑
添加记录时,用户必须为所有列提供一个值。并且记录始终附加到 model_id。
因此,提供什么其他条件并不重要,因为模型是关键。
最佳答案
- 两次查询的结果不一定相同。条件越多,行数可能越少。
- 作为一般规则,您不能说更多的条件会使它更快。在某些情况下,这是可能的,但不是很可能。这完全取决于您在表上定义的索引类型。
这是 one of my older answer on multi-column where clauses and indexes in MySQL .
关于mysql - 查询速度,基于条件的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18697030/