2017-08-28 12 views
0

現在、私はCOUNT(*)検索をしようとしている200k +レコードのテーブルを持っています。検索は送信データフェーズに達するまでは非常に高速です。MySQL "データを送信中"非常に遅い

CREATE TABLE `evento_inscricao_participante_ingresso` (
`id` int(11) NOT NULL AUTO_INCREMENT, 
`id_participante` int(11) DEFAULT NULL, 
`id_carrinho` int(11) DEFAULT NULL, 
`id_cortesia` int(11) DEFAULT NULL, 
`codigo` varchar(255) DEFAULT NULL, 
`checkin` datetime DEFAULT NULL, 
`criado_em` datetime DEFAULT NULL, 
`modificado_em` datetime DEFAULT NULL, 
`criado_por` int(11) DEFAULT NULL, 
`modificado_por` int(11) DEFAULT NULL, 
`deletado` int(1) DEFAULT '0', 
`deletado_por` int(11) DEFAULT NULL, 
`deletado_em` datetime DEFAULT NULL, 
PRIMARY KEY (`id`), 
UNIQUE KEY `codigo_UNIQUE` (`codigo`), 
KEY `fk_evento_inscricao_participante_ingresso_evento_inscricao__idx` (`id_participante`), 
KEY `fk_evento_inscricao_participante_ingresso_evento_inscricao__idx1` (`id_carrinho`), 
KEY `fk_evento_inscricao_participante_ingresso_evento_cortesia1_idx` (`id_cortesia`), 
CONSTRAINT `fk_evento_inscricao_participante_ingresso_evento_inscricao_ca1` FOREIGN KEY (`id_carrinho`) REFERENCES `evento_inscricao_carrinho` (`id`) ON DELETE SET NULL ON UPDATE SET NULL, 
CONSTRAINT `fk_evento_inscricao_participante_ingresso_evento_inscricao_pa1` FOREIGN KEY (`id_participante`) REFERENCES `evento_inscricao_participante` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB AUTO_INCREMENT=175477 DEFAULT CHARSET=utf8; 

クエリ

SELECT COUNT(*) 
      FROM evento_inscricao_participante_ingresso t 
      LEFT JOIN evento_inscricao_carrinho c ON (c.id = t.id_carrinho) 
      LEFT JOIN evento_inscricao_participante p ON (p.id = t.id_participante) 
      LEFT JOIN evento_ingresso i ON (i.id = c.id_ingresso) 
      LEFT JOIN evento_inscricao ins ON (ins.id = c.id_inscricao) 
      LEFT JOIN evento_cortesia cortesia ON (t.id_cortesia = cortesia.id) 
      WHERE (i.id_evento = 894 
      AND i.id = 3255 AND ins.id_status = 1) 
      OR (cortesia.id_evento = 894 
      AND cortesia.id_ingresso = 3255 AND t.id_cortesia IS NOT NULL) 

プロフィール

starting 0.000104 
checking permissions 0.000004 
checking permissions 0.000002 
checking permissions 0.000002 
checking permissions 0.000001 
checking permissions 0.000002 
checking permissions 0.000004 
Opening tables 0.000048 
System lock 0.000010 
init 0.000029 
optimizing 0.000016 
statistics 0.000035 
preparing 0.000020 
executing 0.000004 
Sending data 18.216500 
end 0.000010 
query end 0.000004 
closing tables 0.000013 
freeing items 0.000106 
logging slow query 0.000004 
cleaning up 0.000004 

説明

1 SIMPLE t ALL fk_evento_inscricao_participante_ingresso_evento_cortesia1_idx NULL NULL NULL 205120 NULL 
1 SIMPLE c eq_ref PRIMARY PRIMARY 4 ingresso_main.t.id_carrinho 1 NULL 
1 SIMPLE p eq_ref PRIMARY PRIMARY 4 ingresso_main.t.id_participante 1 Using index 
1 SIMPLE i eq_ref PRIMARY PRIMARY 4 ingresso_main.c.id_ingresso 1 Using where 
1 SIMPLE ins eq_ref PRIMARY PRIMARY 4 ingresso_main.c.id_inscricao 1 Using where 
1 SIMPLE cortesia eq_ref PRIMARY PRIMARY 4 ingresso_main.t.id_cortesia 1 Using where 

だから私はすでにインデックスを作成した場合、多くの時間を費やし、なぜ私は理解していないもの。古いレコードを削除すると、時間の改善は50%になります。私はで使用されるすべてのテーブルのインデックス

(アマゾンRDS/m3.medium/5.6.35)とローカルホスト(MySQLの5.6.35)の生産に参加テスト:

evento_inscricao_carrinho 0 PRIMARY 1 id A 76098 NULL NULL  BTREE  
evento_inscricao_carrinho 1 FK_incricao_carrinho_x_evento_ingresso 1 id_ingresso A 76098 NULL NULL YES BTREE  
evento_inscricao_carrinho 1 FK_incricao_carrinho_x_usuario 1 id_usuario A 76098 NULL NULL YES BTREE  
evento_inscricao_carrinho 1 FK_evento_inscricao_carrinho_x_evento_inscricao 1 id_inscricao A 76098 NULL NULL YES BTREE  
evento_inscricao_carrinho 1 fk_evento_inscricao_carrinho_evento_codigo_desconto1_idx 1 id_codigo_desconto A 76098 NULL NULL YES BTREE  
evento_inscricao_carrinho 1 fk_evento_inscricao_carrinho_evento_convite_envio1_idx 1 id_convite_restrito A 76098 NULL NULL YES BTREE  

evento_inscricao_participante 0 PRIMARY 1 id A 69614 NULL NULL  BTREE  
evento_inscricao_participante 1 fk_evento_inscricao_participante_evento_inscricao1_idx 1 id_inscricao A 69614 NULL NULL YES BTREE  
evento_inscricao_participante 1 fk_evento_inscricao_participante_evento_inscricao_carrinho1_idx 1 id_carrinho A 69614 NULL NULL YES BTREE  


evento_ingresso 0 PRIMARY 1 id A 4021 NULL NULL  BTREE  
evento_ingresso 1 FK_evento_ingresso_x_inquilino 1 id_inquilino A 670 NULL NULL YES BTREE  
evento_ingresso 1 FK_evento_ingresso_x_evento 1 id_evento A 4021 NULL NULL YES BTREE  
evento_ingresso 1 fk_evento_ingresso_evento_ingresso1_idx 1 id_ingresso_lote A 6 NULL NULL YES BTREE  

evento_inscricao 0 PRIMARY 1 id A 90011 NULL NULL  BTREE  
evento_inscricao 1 FK_evento_incricao_x_tipo_pagamento 1 id_pagamento_tipo A 90011 NULL NULL YES BTREE  
evento_inscricao 1 FK_evento_inscricao_x_evento 1 id_evento A 90011 NULL NULL YES BTREE  
evento_inscricao 1 FK_evento_inscricao_x_usuario 1 id_usuario A 90011 NULL NULL YES BTREE  
evento_inscricao 1 fk_evento_inscricao_evento_inscricao_status1_idx 1 id_status A 90011 NULL NULL YES BTREE  
evento_inscricao 1 fk_evento_inscricao_evento_recorrencia1_idx 1 id_recorrencia A 90011 NULL NULL YES BTREE  
evento_inscricao 1 fk_evento_inscricao_terminal1_idx 1 id_terminal A 90011 NULL NULL YES BTREE  

evento_cortesia 0 PRIMARY 1 id A 778 NULL NULL  BTREE  
evento_cortesia 1 FK_evento_apoio_x_evento 1 id_evento A 129 NULL NULL YES BTREE  
evento_cortesia 1 fk_evento_cortesia_evento_recorrencia1_idx 1 id_recorrencia A 1 NULL NULL YES BTREE  
evento_cortesia 1 fk_evento_cortesia_evento_ingresso1_idx 1 id_ingresso A 259 NULL NULL YES BTREE  
evento_cortesia 1 fk_evento_cortesia_terminal1_idx 1 id_terminal A 21 NULL NULL YES BTREE  
+0

あなたは、私たちにあなたがジョインとメインテーブルで言及したすべてのテーブルからショーインデックスを提供できますか? – Noob

+1

あなたの質問にはpastebinのようなオフサイトのリソースを使用しないでください。ここでの目的は、他の人にあなたの経験から学ばせることです。重要な情報をサイト外に埋めれば、それを打ち負かすことができます。 –

+0

申し訳ありませんが、私はインデックスで私の投稿を編集します。 – bertu

答えて

1

あなたがきた指標JOINを手助けしていますが、WHEREの条件の大部分を評価するには、COUNT()を計算するためにMySQLがテーブルの200k行のほとんどを反復処理する必要があります。これには時間がかかります。

データベースの構造を大幅に変更することなく、このクエリをより高速に最適化することはできません。

+0

「重要な変化」はどういう意味ですか?別のテーブルを作成しますか? – bertu

+0

あなたは何を変更するか説明できますか? @ duskwuff私はこのテーマについて自分自身を改善したいです。もしあなたが私たちにもっと情報を与えることができれば。私は、クエリが検索する必要がある場合は20%>のテーブルの高速ですが、クエリでインデックスを使用していない – Noob

関連する問題