English 中文(简体)
How to optimize this mysql query - explain output included
原标题:

This is the query (a search query basically, based on tags):-

select
SUM(DISTINCT(ttagrels.id_tag in (2105,2120,2151,2026,2046) )) as key_1_total_matches, td.*, u.* 
from Tutors_Tag_Relations AS ttagrels
Join Tutor_Details AS td ON td.id_tutor = ttagrels.id_tutor
JOIN Users as u on u.id_user = td.id_user 
where  (ttagrels.id_tag in (2105,2120,2151,2026,2046)) group by td.id_tutor HAVING key_1_total_matches = 1

And following is the database dump needed to execute this query:-

CREATE TABLE IF NOT EXISTS `Users` (
  `id_user` int(10) unsigned NOT NULL auto_increment,
  `id_group` int(11) NOT NULL default  0 ,

  PRIMARY KEY  (`id_user`),
  KEY `Users_FKIndex1` (`id_group`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=730 ;

INSERT INTO `Users` (`id_user`, `id_group`) VALUES
(303, 1);


CREATE TABLE IF NOT EXISTS `Tutor_Details` (
  `id_tutor` int(10) unsigned NOT NULL auto_increment,
  `id_user` int(10) NOT NULL default  0 ,

  PRIMARY KEY  (`id_tutor`),
  KEY `Users_FKIndex1` (`id_user`),
  KEY `id_user` (`id_user`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=58 ;

INSERT INTO `Tutor_Details` (`id_tutor`, `id_user`) VALUES
(26, 303);


CREATE TABLE IF NOT EXISTS `Tags` (
  `id_tag` int(10) unsigned NOT NULL auto_increment,
  `tag` varchar(255) default NULL,
  PRIMARY KEY  (`id_tag`),
  UNIQUE KEY `tag` (`tag`),
  KEY `id_tag` (`id_tag`),
  KEY `tag_2` (`tag`),
  KEY `tag_3` (`tag`),
  KEY `tag_4` (`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2957 ;


INSERT INTO `Tags` (`id_tag`, `tag`) VALUES
(2026,  Brendan.
In ),
(2046,  Brendan. ),
(2105,  Brendan ),
(2120,  Brendan  s ),
(2151,  Brendan) );


CREATE TABLE IF NOT EXISTS `Tutors_Tag_Relations` (
  `id_tag` int(10) unsigned NOT NULL default  0 ,
  `id_tutor` int(10) unsigned default NULL,
  `tutor_field` varchar(255) default NULL,
  `cdate` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `udate` timestamp NULL default NULL,
  KEY `Tutors_Tag_Relations` (`id_tag`),
  KEY `id_tutor` (`id_tutor`),
  KEY `id_tag` (`id_tag`),
  KEY `id_tutor_2` (`id_tutor`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `Tutors_Tag_Relations` (`id_tag`, `id_tutor`, `tutor_field`, `cdate`, `udate`) VALUES
(2105, 26,  firstname ,  2010-06-17 17:08:45 , NULL);

ALTER TABLE `Tutors_Tag_Relations`
  ADD CONSTRAINT `Tutors_Tag_Relations_ibfk_2` FOREIGN KEY (`id_tutor`) REFERENCES `Tutor_Details` (`id_tutor`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  ADD CONSTRAINT `Tutors_Tag_Relations_ibfk_1` FOREIGN KEY (`id_tag`) REFERENCES `Tags` (`id_tag`) ON DELETE NO ACTION ON UPDATE NO ACTION;

What the query does? This query actually searches tutors which contain "Brendan"(as their name or biography or something). The id_tags 2105,2120,2151,2026,2046 are nothing but the tags which are LIKE "%Brendan%".

My question is :-

1.In the explain of this query, the reference column shows NULL for ttagrels, but there are possible keys (Tutors_Tag_Relations,id_tutor,id_tag,id_tutor_2). So, why is no key being taken. How to make the query take references. Is it possible at all? 2. The other two tables td and u are using references. Any indexing needed in those? I think not.

Check the explain query output here http://www.test.examvillage.com/explain.png

问题回答

Don t analyze performance of database with single record in table. Create at least 100 records.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签