我有一個查詢,它似乎很慢
我的問題
select conversation_hash as search_hash
from conversation
where conversation_hash ='xxxxx'
and result_published_at between '1600064000' and '1610668799'
order by result_published_at desc
limit 5
773179我跑步時總共有記錄
select count(*)
from conversation
where conversation_hash ='xxxxx'
在我做一個解釋查詢之后
explain select conversation_hash as search_hash
from conversation
where conversation_hash ='xxxxx'
and result_published_at between '1600064000' and '1610668799'
order by result_published_at desc
limit 5
我懂了
id,select_type,table,partitions,type,possible_keys,key,key_len,ref,rows,filtered,extra
1, SIMPLE, conversation, , range, idx_result_published_at,conversation_hash_channel_content_id_index,conversation_result_published_at_index,virtaul_ad_id_conversation_hash, idx_result_published_at, 5, , 29383288, 1.79, Using index condition;Using where
可能的問題
- 通過查看解釋查詢,我可以看到它回傳的行數(29383288)比總記錄數(即 773179)多
- key_len 為 5。result_published_at 是時間戳欄位,其長度定義大于 5 eg(1625836640)
我可以改進什么來使這個查詢快速,在此先感謝
編輯
對話索引
Table,Non_unique,Key_name,Seq_in_index,Column_name,Collation,Cardinality,Sub_part,Packed,Null,Index_type,Comment,Index_comment
conversation,0,PRIMARY,1,id,A,96901872,NULL,NULL,,BTREE,,
conversation,0,conversation_conversation_hash_id_result_id_unique,1,conversation_hash_id,A,240485,NULL,NULL,,BTREE,,
conversation,0,conversation_conversation_hash_id_result_id_unique,2,result_id,A,100693480,NULL,NULL,,BTREE,,
conversation,0,conversation_conversation_hash_id_channel_content_id_unique,1,conversation_hash_id,A,232122,NULL,NULL,,BTREE,,
conversation,0,conversation_conversation_hash_id_channel_content_id_unique,2,channel_content_id,A,100693480,NULL,NULL,,BTREE,,
conversation,1,conversation_tool_id_foreign,1,tool_id,A,7788,NULL,NULL,,BTREE,,
conversation,1,idx_result_published_at,1,result_published_at,A,38164712,NULL,NULL,YES,BTREE,,
conversation,1,idx_user_name,1,user_name,A,10896208,NULL,NULL,YES,BTREE,,
conversation,1,conversation_hash_channel_content_id_index,1,conversation_hash,A,294048,NULL,NULL,,BTREE,,
conversation,1,conversation_hash_channel_content_id_index,2,channel_content_id,A,99699696,NULL,NULL,,BTREE,,
conversation,1,idx_parent_channel_content_id,1,parent_channel_content_id,A,3550741,NULL,NULL,YES,BTREE,,
conversation,1,idx_channel_content_id,1,channel_content_id,A,90350472,NULL,NULL,,BTREE,,
conversation,1,conversation_result_published_at_index,1,result_published_at,A,37177476,NULL,NULL,YES,BTREE,,
conversation,1,virtaul_ad_id_conversation_hash,1,conversation_hash,A,238906,NULL,NULL,,BTREE,,
conversation,1,virtaul_ad_id_conversation_hash,2,virtual_ad_id,A,230779,NULL,NULL,YES,BTREE,,
conversation,1,idx_ad_story_id,1,ad_story_id,A,167269,NULL,NULL,YES,BTREE,,
uj5u.com熱心網友回復:
查詢是正確的,看來您必須更新 mysql 的服務器配置,這可能在共享托管環境中不可用。但是,如果您有自己的服務器,請執行以下步驟:
- 轉到
my.cnf檔案,在我的情況下,它托管在/etc/mysql/my.cnf - 創造
query_cache_size,max_connection,innodb_buffer_pool_size,的值innodb_io_capacity - 切換
MyISAM到InnoDB(如果可能) - 使用最新的 MySQL 版本(如果可能)
您可以從這篇文章中獲得更多幫助https://phoenixnap.com/kb/improve-mysql-performance-tuning-optimization
uj5u.com熱心網友回復:
閱讀 Explain 命令的輸出有點困難,因為possible_keys輸出是用逗號分隔的。
根據資料訪問模式,您可能希望在 上創建唯一索引conversation_hash,以防行是唯一的。
如果conversation_hash不是唯一欄位,則可以在其上創建復合索引,conversation_hash, result_published_at以便從索引本身完成查詢。
uj5u.com熱心網友回復:
EXPLAIN 估計行數。(如果不實際運行查詢,就無法獲得確切的行數。)該估計可能比實際計數低很多或高很多。估計很少有那么遠,但我不會擔心,只是生氣。
Text 和 Blob 列的存在有時會增加 Explain 的不精確性。
關鍵長度:
- 原始長度,為 5
TIMESTAMP(更多見下文)。 - 1 表示 if
NULL( 0 表示NOT NULL)。 - 不是很有用
VARCHAR。
在舊版本的 MySQL 中,Timestamp 占用 4 個位元組,DATETIME 占用 8 個位元組。當添加小數秒時,在這兩種情況下,這些數字都更改為 5。這允許使用“長度”來指示小數位數。并且日期時間從壓縮十進制更改為整數。
建議你跑ANALYZE TABLE。這可能會改進用于估計的基礎統計資料。
請提供SHOW CREATE TABLE;它可能會提供更多見解。
'composite' INDEX(conversation_hash, result_published_at),按該順序,對于該查詢是最佳的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/445186.html
標籤:mysql
上一篇:如何在Mysql中創建復合外鍵
下一篇:如何使用sql獲取剩余的總庫存
