mysql - 查询速度,基于条件的数量?

标签 mysql sql select optimization multiple-conditions

我有一张 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。
因此,提供什么其他条件并不重要,因为模型是关键。

最佳答案

  1. 两次查询的结果不一定相同。条件越多,行数可能越少。
  2. 作为一般规则,您不能说更多的条件会使它更快。在某些情况下,这是可能的,但不是很可能。这完全取决于您在表上定义的索引类型。

这是 one of my older answer on multi-column where clauses and indexes in MySQL .

关于mysql - 查询速度,基于条件的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18697030/

相关文章:

如果日期不存在,MySQL 选择具有预定义值的日期

php - 下拉框包含空白空间/值

mysql - 将表名传递给游标中的 select 语句

sql - 确定遍历一个表并更新另一个表的最佳方法

mysql - 检查行中相邻列中是否有相同的条目

jquery - 选择2 : Showing multiple selected values

Oracle - 不能在 select 子句中对其他列使用 * 符号

mysql - 如何从mysql中的两个表中选择数据

sql - 如何计算包含列的非聚集索引的 Num_Key_Cols ?

mysql - 改进查询