[英]executing queries with and without indexes in mysql
我必須在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
。
我執行此操作,兩次都得到0行影響的結果。 這個可以嗎?
是的,沒關系。
有DDL操作創建新對象,不應輸出任何內容。
因為當我執行查詢時(在創建索引之后),我需要與之前相同的時間(沒有索引)
查詢不得使用索引。 內部優化器根據數據分布做出決策。 例如,如果column_name.table_1有50個唯一值,則不使用index。
更多詳細信息可以在官方文檔中找到: 9.3.1 MySQL如何使用索引
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.