1. Below query is taking long time around 540 seconds.

  2. it was not having any indexes on fields. i was created indexes on where clause and other fields and tried but the query is not using indexes no luck.

  3. can any one please suggest me.

Please find explain plan and Table structure here.

 mysql>  explain select alias_to, fake_uri, concat(ifnull(parameters,''),ifnull(parameters2,''),ifnull(parameters3,'')) as parameters, precedence, template_name from site_map_template  order by precedence, fake_uri desc;
+----+-------------+-------------------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table             | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+-------------------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | site_map_template | ALL  | NULL          | NULL | NULL    | NULL | 7616 | Using filesort |
+----+-------------+-------------------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)

mysql> show create table site_map_template\G
*************************** 1. row ***************************
       Table: site_map_template
Create Table: CREATE TABLE `site_map_template` (
  `row_mod` datetime DEFAULT NULL,
  `row_create` datetime DEFAULT NULL,
  `template_name` varchar(50) COLLATE latin1_bin DEFAULT NULL,
  `alias_to` varchar(100) COLLATE latin1_bin DEFAULT NULL,
  `package` varchar(50) COLLATE latin1_bin DEFAULT NULL,
  `fake_uri` varchar(255) COLLATE latin1_bin DEFAULT NULL,
  `precedence` int(11) DEFAULT NULL,
  `parameters` varchar(255) COLLATE latin1_bin DEFAULT NULL,
  `parameters2` varchar(255) COLLATE latin1_bin DEFAULT NULL,
  `parameters3` varchar(255) COLLATE latin1_bin DEFAULT NULL,
  KEY `idx_1372` (`template_name`),
  KEY `idx_n1` (`precedence`),
  KEY `idx_n2` (`fake_uri`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin

asked Feb 18 '13 at 11:39

Ravi%20Kiran's gravatar image

Ravi Kiran
1111


2 Answers:

Try to add an index with the columns "precedence" and "fake_uri".

If the database don't use your index, the optimizer possibly think it's faster to do a full table scan. You can add "force index(yourindexname)" after "from site_ map_template" and then the query has to use this index. This is not an optimal solution because it could be faster without this index or with another index. With "use index()" the query may use this index if it's faster for the optimizer (I think). I had some simple queries which don't use the index because it's no benefit.

If I'm wrong with the optimizer, correct me.

answered Feb 23 '13 at 17:25

sql_sniffer's gravatar image

sql_sniffer
1

The same example could work in Oracle. Oracle may try to combine two indexes before and use the result while accessing a table.

answered Sep 03 '13 at 22:10

nmaqsudov's gravatar image

nmaqsudov
12