努力让mysql 5.5查询与现有索引一起高效工作,这对我有限的知识和经验应该足以完成这项工作。该查询目前执行得足够快,但在扩展之前我宁愿做到这一点。
表格结构:
CREATE TABLE tasks (
taskid INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
clientid INT(10) UNSIGNED NOT NULL,
taskdata CHAR(40) NOT NULL,
tasktype SMALLINT(5) UNSIGNED NOT NULL,
clientstaskref CHAR(40) NOT NULL,
deadline DATETIME NOT NULL,
canbeongoing BIT(1) NOT NULL,
isunderway BIT(1) NOT NULL,
checkenabled BIT(1) NOT NULL,
lastupdated DATETIME NOT NULL,
PRIMARY KEY (taskid),
UNIQUE KEY clientstaskref (clientstaskref),
UNIQUE KEY checkenabled (checkenabled,clientid,taskid),
KEY deadline (deadline),
KEY lastupdated (lastupdated),
KEY isunderway (isunderway),
KEY taskdata (taskdata),
KEY tasktype (tasktype),
KEY clientid (clientid,tasktype)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE tasktypes (
typeid SMALLINT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
subtypeid SMALLINT(5) UNSIGNED NOT NULL,
typedesc CHAR(40) NOT NULL,
rectype CHAR(40) NOT NULL,
checkenabled BIT(1) NOT NULL,
firstadded DATETIME NOT NULL,
lastupdated DATETIME NOT NULL,
isaccountable BIT(1) NOT NULL,
PRIMARY KEY (typeid),
UNIQUE KEY checkenabled (checkenabled,subtypeid,typedesc),
UNIQUE KEY typeid (typeid,typedesc),
UNIQUE KEY subtypeid (subtypeid,typedesc),
KEY typedesc (typedesc)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
流氓查询:
SELECT a.taskid, a.taskdata, b.typedesc, a.deadline, a.checkenabled
FROM tasks AS a, tasktypes AS b
WHERE a.clientid = 220
AND a.tasktype = b.typeid
ORDER BY b.typedesc
解释输出:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE a ref tasktype,clientid clientid 4 const 56 Using temporary; Using filesort
1 SIMPLE b eq_ref PRIMARY,typeid PRIMARY 2 testing.a.tasktype 1
显然我想摆脱使用临时;使用filesort!
道歉,如果这复制了一个现有的问题,但是有很多类似的问题,看起来需要数周的时间来确定一个提供解决方案的问题!
答案 0 :(得分:0)
<强> Unpanic 强>
不,你应该摆脱“使用临时;使用filesort”这一点并不明显。它们是执行查询的最快方式。
让我们剖析一下......
你是JOINing
两张桌子。但是你过滤了一个(a.clientid
)而另一个(b.typedesc
)排序。避免使用tmp + filesort的唯一方法是使用一个涵盖所有WHERE
和ORDER BY
子句的索引。这是不可能的。
因此,优化器首先决定对a.clientid
进行“过滤”,希望将有趣行数减少到估计值56.然后将JOINs
更改为另一个表中的eq_ref..PRIMARY
。最佳方式(MEMORY
)。最后它排序了~56行。
不要绝望。 “使用临时;使用filesort”是用词不当。如果一切顺利,“temp”将是一个in-RAM CHAR(..)
表,而“filesort”将不会触及任何“文件”,而只是执行一个内存表的qsort。
其他一些提示 ...
VARCHAR(..)
。 isunderway
可能更好。JOIN...ON...
),这样的索引几乎从未使用过。PRIMARY KEY (typeid)
语法。UNIQUE KEY typeid (typeid,typedesc)
和PRIMARY KEY
:
UNIQUE KEY
是kitchen list
>>>>>> ------Exception-------
>>>>>> Class: Kitchen::ClientError
>>>>>> Message: Could not load the 'ssh' driver from the load path. Please ensure that your driver is installed as a gem or included in your Gemfile if using Bundler.
>>>>>> ----------------------
>>>>>> Please see .kitchen/logs/kitchen.log for more details
>>>>>> Also try running `kitchen diagnose --all` for configuration
cat .kitchen/logs/kitchen.log
E, [2015-09-15T16:48:11.630689 #30421] ERROR -- Kitchen: ------Exception-------
E, [2015-09-15T16:48:11.630833 #30421] ERROR -- Kitchen: Class: Kitchen::ClientError
E, [2015-09-15T16:48:11.630894 #30421] ERROR -- Kitchen: Message: Could not load the 'ssh' driver from the load path. Please ensure that your driver is installed as a gem or included in your Gemfile if using Bundler.
E, [2015-09-15T16:48:11.630936 #30421] ERROR -- Kitchen: ---Nested Exception---
E, [2015-09-15T16:48:11.630974 #30421] ERROR -- Kitchen: Class: LoadError
E, [2015-09-15T16:48:11.631009 #30421] ERROR -- Kitchen: Message: cannot load such file -- kitchen/driver/ssh
E, [2015-09-15T16:48:11.631045 #30421] ERROR -- Kitchen: ------Backtrace-------
E, [2015-09-15T16:48:11.631148 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/apps/test-kitchen/lib/kitchen/driver.rb:50:in `rescue in for_plugin'
E, [2015-09-15T16:48:11.631195 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/apps/test-kitchen/lib/kitchen/driver.rb:40:in `for_plugin'
E, [2015-09-15T16:48:11.631233 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/apps/test-kitchen/lib/kitchen/config.rb:226:in `new_driver'
E, [2015-09-15T16:48:11.631299 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/apps/test-kitchen/lib/kitchen/config.rb:239:in `new_instance'
E, [2015-09-15T16:48:11.631340 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/apps/test-kitchen/lib/kitchen/config.rb:135:in `block in build_instances'
E, [2015-09-15T16:48:11.631376 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/apps/test-kitchen/lib/kitchen/config.rb:134:in `map'
E, [2015-09-15T16:48:11.631412 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/apps/test-kitchen/lib/kitchen/config.rb:134:in `with_index'
E, [2015-09-15T16:48:11.631448 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/apps/test-kitchen/lib/kitchen/config.rb:134:in `build_instances'
E, [2015-09-15T16:48:11.631484 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/apps/test-kitchen/lib/kitchen/config.rb:110:in `instances'
E, [2015-09-15T16:48:11.631520 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/apps/test-kitchen/lib/kitchen/command.rb:115:in `filtered_instances'
E, [2015-09-15T16:48:11.631557 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/apps/test-kitchen/lib/kitchen/command.rb:145:in `parse_subcommand'
E, [2015-09-15T16:48:11.631594 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/apps/test-kitchen/lib/kitchen/command/list.rb:32:in `call'
E, [2015-09-15T16:48:11.631630 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/apps/test-kitchen/lib/kitchen/cli.rb:56:in `perform'
E, [2015-09-15T16:48:11.631667 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/apps/test-kitchen/lib/kitchen/cli.rb:108:in `list'
E, [2015-09-15T16:48:11.631703 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/lib/ruby/gems/2.1.0/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
E, [2015-09-15T16:48:11.631739 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/lib/ruby/gems/2.1.0/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
E, [2015-09-15T16:48:11.631775 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/apps/test-kitchen/lib/kitchen/cli.rb:308:in `invoke_task'
E, [2015-09-15T16:48:11.631811 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/lib/ruby/gems/2.1.0/gems/thor-0.19.1/lib/thor.rb:359:in `dispatch'
E, [2015-09-15T16:48:11.631847 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/lib/ruby/gems/2.1.0/gems/thor-0.19.1/lib/thor/base.rb:440:in `start'
E, [2015-09-15T16:48:11.631914 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/apps/test-kitchen/bin/kitchen:13:in `block in <top (required)>'
E, [2015-09-15T16:48:11.631976 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/apps/test-kitchen/lib/kitchen/errors.rb:154:in `with_friendly_errors'
E, [2015-09-15T16:48:11.632032 #30421] ERROR -- Kitchen: /opt/chefdk/embedded/apps/test-kitchen/bin/kitchen:13:in `<top (required)>'
E, [2015-09-15T16:48:11.632098 #30421] ERROR -- Kitchen: /usr/bin/kitchen:15:in `load'
E, [2015-09-15T16:48:11.632141 #30421] ERROR -- Kitchen: /usr/bin/kitchen:15:in `<main>'
E, [2015-09-15T16:48:11.632204 #30421] ERROR -- Kitchen: ----------------------
。将第二列添加到唯一键不会使其“更独特”或其他任何有用的。 InnoDB表聚集在PK周围,因此从PK开始的另一个索引几乎总是浪费。 DROP那个UNIQUE键。