我必须在 mysql 中使用和不使用索引运行相同的查询。 我这样创建索引:
create index index_1 on table_1(column_name);
create index index_2 on table_2(column_name);
我执行此操作,两次都得到 0 行受到影响的结果。这样可以吗?
因为当我执行查询时(在我创建索引之后),它花费的时间和以前一样(没有索引。)
我有多个关于这个数据库的小查询,例如
SELECT DISTINCT customers.customer_id, customers.customer_name
FROM customers
INNER JOIN accounts ON customers.customer_id = accounts.customer_id
INNER JOIN transactions ON transactions.account_id = accounts.account_id
WHERE transactions.trn_date >= '2011/05/01'
AND transactions.trn_date <= '2011/05/31'
ORDER BY customers.customer_id
最佳答案
该查询需要这些索引:
transactions: INDEX(trn_date)
accounts: INDEX(account_id)
customers: INDEX(customer_id)
在最后两种情况下,您可能已经将该列作为 PRIMARY KEY
。如果是这样,请不要添加多余的 INDEX
。
关于mysql - 在 mysql 中执行带索引和不带索引的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37184597/