1

我有两张桌子:

第一个:

    如果不存在“标签”,则创建表(
  `i` int(10) NOT NULL AUTO_INCREMENT,
  `id` int(10) NOT NULL,
  `k` varchar(32) 整理 utf8_bin 不为空,
  主键 (`i`),
  键`k`(`k`)
) 引擎=MyISAM 默认字符集=utf8 排序=utf8_bin AUTO_INCREMENT=1505426 ;

第二:

如果不存在则创建表 `w` (
  `id` int(7) NOT NULL,
  `title` varchar(255) 整理 utf8_bin 不为空,
  `k1` varchar(24) 整理 utf8_bin 不为空,
  `k2` varchar(24) 整理 utf8_bin 不为空,
  `k3` varchar(24) 整理 utf8_bin 不为空,
  `k4` varchar(24) 整理 utf8_bin 不为空,
  `k5` varchar(24) 整理 utf8_bin 不为空,
  `k6` varchar(24) 整理 utf8_bin 不为空,
  `k7` varchar(24) 整理 utf8_bin 不为空,
  `k8` varchar(24) 整理 utf8_bin 不为空,
  `w` int(5) NOT NULL,
  `h` int(5) NOT NULL,
  `s` varchar(32) 整理 utf8_bin 不为空,
  `r` varchar(11) 整理 utf8_bin 不为空,
  `v` int(7) 非空,
  `c` varchar(32) 整理 utf8_bin 不为空,
  `c1` varchar(6) 整理 utf8_bin 不为空,
  `c2` varchar(6) 整理 utf8_bin 不为空,
  `c3` varchar(6) 整理 utf8_bin 不为空,
  `c4` varchar(6) 整理 utf8_bin 非空,
  `c5` varchar(6) 整理 utf8_bin 不为空,
  `c6` varchar(6) 整理 utf8_bin 不为空,
  `m` varchar(4) 整理 utf8_bin 不为空,
  `t` int(10) 非空,
  `i` int(6) NOT NULL,
  `o` int(6) NOT NULL,
  `f` varchar(255) 整理 utf8_bin 不为空,
  主键(`id`),
  KEY `keywords` (`k1`,`k2`,`k3`,`k4`,`k5`,`k6`,`k7`,`k8`),
  KEY `category` (`c`),
  KEY `color1` (`c1`)
) 引擎=InnoDB 默认字符集=utf8 排序=utf8_bin ROW_FORMAT=DYNAMIC;

I'm trying to search 2 words ($search1 + $search2) from table 'tags' and find id's at 'w' table. query:

SELECT w.id,w.title,w.k1,w.k2,w.k3,w.k4,w.k5,w.k6,w.k7,w.w,w.h,w.s,w.c1,w.c2,w.c3,w.c4,w.c5,w.c6,w.m,w.f
FROM tags,tags as tags2,w
WHERE tags.k ='$search1' AND tags2.k = '$search2'
and tags.id = tags2.id and tags2.id = w.id
ORDER BY `w`.`id`desc limit 24

The problem is that EXPLAIN function shows "Using where; Using temporary; Using filesort" Also I believe that there is another way to optimize this my type search system. Actually all data is in 'w' table, I just have no idea how exactly to search it properly. I would be grateful for any help.

Edit:a bit more information about this

table 'tags' 1505425 records InnoDB = 65,9 MiB

table 'w' 398900 rercords, InnoDB = 140,3 MiB

after this (updated) query:

select `w`.`id` AS `id`,`w`.`title` AS `title`,`w`.`k1` AS `k1`,`w`.`k2` AS `k2`,`w`.`k3` AS `k3`,`w`.`k4` AS `k4`,`w`.`k5` AS `k5`,`w`.`k6` AS `k6`,`w`.`k7` AS `k7`,`w`.`k8` AS `k8`,`w`.`w` AS `w`,`w`.`h` AS `h`,`w`.`s` AS `s`,`w`.`c1` AS `c1`,`w`.`c2` AS `c2`,`w`.`c3` AS `c3`,`w`.`c4` AS `c4`,`w`.`c5` AS `c5`,`w`.`c6` AS `c6`,`w`.`m` AS `m`,`w`.`f` AS `f` from `tags` join `tags` `tags2` join `w` where ((`tags`.`k` = '$search1') and (`tags2`.`id` = `tags`.`id`) and (`w`.`id` = `tags`.`id`) and (`tags2`.`k` = '$search2')) order by `tags`.`i` desc limit 0,24;

mysql still shows:

+----+-------------+------------+--------+---------------+---------+---------+-------------+------+----------+-----------------------------+
| id | select_type | table      | type   | possible_keys | key     | key_len | ref         | rows | filtered | Extra                       |
+----+-------------+------------+--------+---------------+---------+---------+-------------+------+----------+-----------------------------+
|  1 | SIMPLE      | tags       | ref    | k,id          | k       | 98      | const       |  902 |   100.00 | Using where; Using filesort |
|  1 | SIMPLE      | tags2      | ref    | k,id          | id      | 4       | db.tags.id  |    4 |   100.00 | Using where                 |
|  1 | SIMPLE      | w          | eq_ref | PRIMARY       | PRIMARY | 4       | db.tags2.id |    1 |   100.00 | Using where                 |
+----+-------------+------------+--------+---------------+---------+---------+-------------+------+----------+-----------------------------+
3 rows in set, 1 warning (0.00 sec)

一个警告,仍然一切都保持不变,有没有更好的性能解决方案?目前它工作正常,但我认为以后一切都会变得越来越慢。

4

2 回答 2

1

您正在混合存储引擎;mySQL 很难跨存储引擎连接表。

前:

mysql> explain SELECT w.id,w.title,w.k1,w.k2,w.k3,w.k4,w.k5,w.k6,w.k7,w.w,w.h,w.s,w.c1,w.c2,w.c3,w.c4,w.c5,w.c6,w.m,w.f FROM tags,tags as tags2,w WHERE tags.k ='$search1' AND tags2.k = 'search2' and tags.id = tags2.id and tags2.id = w.id ORDER BY `w`.`id`desc limit 24;
+----+-------------+-------+--------+---------------+---------+---------+-------------+------+----------------------------------------------+
| id | select_type | table | type   | possible_keys | key     | key_len | ref         | rows | Extra                                        |
+----+-------------+-------+--------+---------------+---------+---------+-------------+------+----------------------------------------------+
|  1 | SIMPLE      | tags  | ref    | k             | k       | 98      | const       |    1 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | tags2 | ref    | k             | k       | 98      | const       |    1 | Using where                                  |
|  1 | SIMPLE      | w     | eq_ref | PRIMARY       | PRIMARY | 4       | tmp.tags.id |    1 |                                              |
+----+-------------+-------+--------+---------------+---------+---------+-------------+------+----------------------------------------------+

将 w 更改为 MyISAM:

mysql> alter table w engine MyISAM;
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

后:

mysql> explain SELECT w.id,w.title,w.k1,w.k2,w.k3,w.k4,w.k5,w.k6,w.k7,w.w,w.h,w.s,w.c1,w.c2,w.c3,w.c4,w.c5,w.c6,w.m,w.f FROM tags,tags as tags2,w WHERE tags.k ='$search1' AND tags2.k = 'search2' and tags.id = tags2.id and tags2.id = w.id ORDER BY `w`.`id`desc limit 24;
+----+-------------+-------+--------+---------------+------+---------+-------+------+-------------+
| id | select_type | table | type   | possible_keys | key  | key_len | ref   | rows | Extra       |
+----+-------------+-------+--------+---------------+------+---------+-------+------+-------------+
|  1 | SIMPLE      | w     | system | PRIMARY       | NULL | NULL    | NULL  |    1 |             |
|  1 | SIMPLE      | tags  | ref    | k             | k    | 98      | const |    1 | Using where |
|  1 | SIMPLE      | tags2 | ref    | k             | k    | 98      | const |    1 | Using where |
+----+-------------+-------+--------+---------------+------+---------+-------+------+-------------+
3 rows in set (0.00 sec)
于 2012-11-04T18:05:04.477 回答
0

Vagiz,您更新的查询似乎没问题:

select `w`.`id` AS `id`,`w`.`title` AS `title`,`w`.`k1` AS `k1`,`w`.`k2` AS `k2`,
`w`.`k3` AS `k3`,`w`.`k4` AS `k4`,`w`.`k5` AS `k5`,`w`.`k6` AS `k6`,`w`.
`k7` AS `k7`,`w`.`k8` AS `k8`,`w`.`w` AS `w`,`w`.`h` AS `h`,`w`.`s` AS `s`,
`w`.`c1` AS `c1`,`w`.`c2` AS `c2`,`w`.`c3` AS `c3`,`w`.`c4` AS `c4`,
`w`.`c5` AS `c5`,`w`.`c6` AS `c6`,`w`.`m` AS `m`,`w`.`f` AS `f`
from `tags` join `tags` `tags2` join `w` where ((`tags`.`k` = '$search1') 
and (`tags2`.`id` = `tags`.`id`) and (`w`.`id` = `tags`.`id`) 
and (`tags2`.`k` = '$search2')) order by `tags`.`i` desc limit 0,24;

如果它产生以下解释结果:

+----+-------------+------------+--------+---------------+---------+---------+-------------+------+----------+-----------------------------+
| id | select_type | table      | type   | possible_keys | key     | key_len | ref         | rows | filtered | Extra                       |
+----+-------------+------------+--------+---------------+---------+---------+-------------+------+----------+-----------------------------+
|  1 | SIMPLE      | tags       | ref    | k,id          | k       | 98      | const       |  902 |   100.00 | Using where; Using filesort |
|  1 | SIMPLE      | tags2      | ref    | k,id          | id      | 4       | db.tags.id  |    4 |   100.00 | Using where                 |
|  1 | SIMPLE      | w          | eq_ref | PRIMARY       | PRIMARY | 4       | db.tags2.id |    1 |   100.00 | Using where                 |
+----+-------------+------------+--------+---------------+---------+---------+-------------+------+----------+-----------------------------+
3 rows in set, 1 warning (0.00 sec)

不用担心文件排序和性能。来自Mysql 性能博客

事实是,文件排序的名字很糟糕。任何时候无法从索引执行排序,它都是文件排序。它与文件无关。文件排序应该称为“排序”。它本质上是快速排序。

于 2012-11-04T23:14:00.437 回答