簡體   English   中英

什么是復合索引,如何正確使用?

[英]What is compound indexing and how do I use it properly?

我有一個非常慢的查詢,重復了很多次。 我嘗試索引各個字段,但似乎無濟於事。 CPU使用率仍然很高,查詢仍然顯示在慢查詢日志中。 似乎我需要復合索引?

如何正確索引以下查詢?

select *
from `to_attachments` left join
     `attachments`
     on `to_attachments`.`attachment_id` = `attachments`.`id`
where `to_attachments`.`object_type` = 'communicator' and `to_attachments`.`object_id` = '64328'
order by `attachments`.`created_at` desc;

解釋結果: 1 SIMPLE to_attachments index NULL PRIMARY 775 NULL 244384 Using where; Using index; Using temporary; Using filesort 1 SIMPLE attachments eq_ref PRIMARY PRIMARY 4 quote.to_attachments.attachment_id 1 NULL 1 SIMPLE to_attachments index NULL PRIMARY 775 NULL 244384 Using where; Using index; Using temporary; Using filesort 1 SIMPLE attachments eq_ref PRIMARY PRIMARY 4 quote.to_attachments.attachment_id 1 NULL

to_attachments的索引 在此處輸入圖片說明

您想要在to_attachments(object_type, object_id, attachment_id)attachments(id)to_attachments(object_type, object_id, attachment_id)索引。

您的索引順序錯誤,應該是(object_type,object_id,attachment_id)。 在多列索引中,索引中列的順序為MATTER。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM