2013-04-08 74 views
0

我对着在查询执行这里有些问题是我的情况:的Hibernate查询执行问题

我有两个表日志 2十万条记录和的LogRecords 6十万条记录

其中单个日志在日志表记录可以有多个日志消息的LogRecords表我的数据库架构如下

日志

CREATE TABLE `log` (       
     `logid` varchar(50) NOT NULL DEFAULT '',   
     `creationtime` bigint(20) DEFAULT NULL,    
     `serviceInitiatorID` varchar(50) DEFAULT NULL, 
     PRIMARY KEY (`logid`),       
     KEY `idx_creationtime_wsc_log` (`creationtime`) 
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 

而且的LogRecords

CREATE TABLE `logrecords` (             
       `logrecordid` varchar(50) NOT NULL DEFAULT '',         
       `timestamp` bigint(20) DEFAULT NULL,           
       `message` varchar(8000) DEFAULT NULL,           
       `loglevel` int(11) DEFAULT NULL,            
       `logid` varchar(50) DEFAULT NULL,            
       `indexcolumn` int(11) DEFAULT NULL,           
       PRIMARY KEY (`logrecordid`),             
       KEY `indx_logrecordid_message_logid` (`logrecordid`,`message`(767),`logid`), 
       KEY `logid` (`logid`),               
       KEY `indx_message` (`message`(767))           
      ) ENGINE=InnoDB DEFAULT CHARSET=latin1  

查询由Hibernate创建就像

select this_.logid as logid4_1_, this_.loglevel as loglevel4_1_, this_.creationtime as creation3_4_1_,this_.serviceInitiatorID as service17_4_1_, this_.logtype as logtype4_1_,logrecord1_.logrecordid as logrecor1_3_0_, logrecord1_.timestamp as timestamp3_0_, logrecord1_.message as message3_0_, logrecord1_.loglevel as loglevel3_0_, logrecord1_.logid as logid3_0_, logrecord1_.indexcolumn as indexcol6_3_0_ from log this_ inner join wsc_logrecords logrecord1_ on this_.logid=logrecord1_.logid where (1=1) and (1=1) and logrecord1_.message like 'SecondMessage' order by this_.creationtime desc limit 25 

其中选修了7313ms执行

查询说明是像

enter image description here

但是,当我执行以下查询它走大约15分钟来执行

select count(*) as y0_ from log this_ inner join logrecords logrecord1_ on this_.logid=logrecord1_.logid where (1=1) and (1=1) and lower(logrecord1_.message) like 'SecondMessage' order by this_.creationtime desc limit 25 

对于上述查询说明是像 enter image description here

和我使用MySQl数据库。我认为索引中存在一些问题或其他我无法识别的问题

任何解决方案将不胜感激。

+0

如果你写'解释之前'查询,它告诉你什么? – 2013-04-08 14:14:07

+0

嗨Shervin我已更新我的问题与查询说明 – Harshil 2013-04-09 04:32:32

回答

0

当您使用lower(logrecord1_.message) like 'SecondMessage'而不是简单的logrecord1_.message like 'SecondMessage'时,数据库引擎将停止使用logrecord1_.message上的索引。

您可以通过创建基于功能的索引来取代logrecord1_.message来解决此问题。

+0

MySQL不支持基于函数的索引,如下所述:http://stackoverflow.com/questions/10595037/is-it-possible-to-have-function- based-index-in-mysql但是,我认为通过对列消息使用不区分大小写的排序规则,我认为lower()函数变得多余。 – Marcellus 2013-04-08 14:25:51

+0

@Marcellus:把它写成答案。我会稍微删除这个答案,因为它没有帮助。 – 2013-04-08 14:49:22

+0

我也尝试使用像'SecondMessage'logrecord1_.message而不是使用较低(logrecord1_.message)像'SecondMessage'计数查询,但没有任何改善。 – Harshil 2013-04-09 07:29:57