MySQL如何遍历IN()和IN()搜索的复合B树索引(How does MySQL traverse a composite B-tree index for IN() AND IN() searches)
我一直在研究索引,无法完全理解MYSQL如何能够使用索引来完成索引
IN() AND IN() ... AND IN()
我正在阅读的书中提到,当我们有索引(
a
,b
,...)但用户想要搜索b
并且我们在a中的基数较低时,我们可以使用技巧并简单地添加IN()WHERE a IN ('x1', 'x2', ... 'all possible values go here') AND `b`>123
假设我们有以下数据
x1 1 x1 4 x1 456 x2 5 x3 1 x3 2 x3 3 x4 1234
它如何遍历此树以满足上面的查询? 它是否只是为IN-s创建所有可能的组合,并且几乎遍历每个查询的树?
WHERE `a`='x1' AND `b`>123 WHERE `a`='x2' AND `b`>123 ...
因此,由于IN-s的数量增加,所有可能的IN组合的数量急剧增加,我们将不得不为这些组合中的每一个运行B树,从而使这个技巧的用处有限? 如果上述情况属实,这是否意味着有一些理论上的观点,即用IN输入索引会比不使用索引慢?
I've been studying indexes and can't completely understand how MYSQL is capable of using indexes at all for statements like
IN() AND IN() ... AND IN()
The book I am reading suggests that when we have an index (
a
,b
, ...) but user wants to search forb
and we have a low cardinality ina
, we can use a trick and simply add IN()WHERE a IN ('x1', 'x2', ... 'all possible values go here') AND `b`>123
Let's say we have the following data
x1 1 x1 4 x1 456 x2 5 x3 1 x3 2 x3 3 x4 1234
How can it traverse this tree to satisfy the query above? Will it simply create all possible combinations for IN-s and will pretty much traverse tree for each of these queries?
WHERE `a`='x1' AND `b`>123 WHERE `a`='x2' AND `b`>123 ...
therefore making this trick of limited usefulness because as number of IN-s increases, the number of all possible IN combinations increases dramatically and we'll have to run through the B-tree for each of these combinations? If the above is true, does this mean that there is some theoretical point where tricking an index with IN will get slower than not using index at all?
原文:https://stackoverflow.com/questions/9568589
最满意答案
我已经对Mongrel源进行了一些调查,结果发现Mongrel安装了一个信号处理程序来捕获标准进程kill(TERM)并正常关机,因此我不需要特殊的程序。
您可以在处理请求时查看它在处理Mongrel时获得的日志输出。 例如:
** TERM signal received. Thu Aug 28 00:52:35 +0000 2008: Reaping 2 threads for slow workers because of 'shutdown' Waiting for 2 requests to finish, could take 60 seconds.Thu Aug 28 00:52:41 +0000 2008: Reaping 2 threads for slow workers because of 'shutdown' Waiting for 2 requests to finish, could take 60 seconds.Thu Aug 28 00:52:43 +0000 2008 (13051) Rendering layoutfalsecontent_typetext/htmlactionindex within layouts/application
I've done a little more investigation into the Mongrel source and it turns out that Mongrel installs a signal handler to catch an standard process kill (TERM) and do a graceful shutdown, so I don't need a special procedure after all.
You can see this working from the log output you get when killing a Mongrel while it's processing a request. For example:
** TERM signal received. Thu Aug 28 00:52:35 +0000 2008: Reaping 2 threads for slow workers because of 'shutdown' Waiting for 2 requests to finish, could take 60 seconds.Thu Aug 28 00:52:41 +0000 2008: Reaping 2 threads for slow workers because of 'shutdown' Waiting for 2 requests to finish, could take 60 seconds.Thu Aug 28 00:52:43 +0000 2008 (13051) Rendering layoutfalsecontent_typetext/htmlactionindex within layouts/application
相关问答
更多-
问题是你在RVM中安装了比你的系统ruby更新版本的“gem”。 降级你的宝石,它应该工作: gem update --system 1.4.1 The problem is that you have a newer version of "gem" installed inside of RVM than you do in your system ruby. Downgrade your gem and it should work: gem update --system 1.4.1
-
为什么要使用Mongrel2?(Why use Mongrel2?)[2023-10-01]
如果你只能命名一件事情,那么Mongrel2将围绕ZeroMQ进行构建,这意味着扩展您的Web服务器从未如此简单。 如果一个请求进来,Mongrel2收到它(在这里没有什么不寻常的,和NginX和任何其他httpd相同)。 接下来的事情是,Mongrel2将编译响应的任务分配给n(ZeroMQ启用)后端,等待他们完成工作,接收结果,编译响应并将其发送给客户端。 现在,魔术就是这样一个事实:n可以是任何数字,并且n中的每一个都可以用ZeroMQ(20左右)所支持的任何语言编写,所有这些都通过网络,所以每个n都 ... -
检查了Mongrel日志文件,看来问题在于我的database.yml文件在应该是mysql2时将适配器指定为sqlite3。 我没有安装sqlite3,因此Mongrel无法启动。 Checked the Mongrel log files and it appears that the issue was that my database.yml file was specifying the adapter as sqlite3 when it should have been mysql2. I d ...
-
哪个Ruby web服务器?(Which Ruby web server? [closed])[2022-04-04]
我是Thin的忠实粉丝。 它速度快(比乘客快),不占用太多内存(少于乘客),并且不太难以设置和维护。 I'm a big fan of Thin. It's fast (faster than passenger), doesn't use much memory (less than passenger), and isn't too hard to set up and maintain. -
您应该可以使用Mongrel GZipFilter执行此操作。 You should be able to do this using the Mongrel GZipFilter.
-
好吧,我已经解决了这个问题(在luislavena本人的帮助下 - 谢谢)。 从https://github.com/luislavena/service_wrapper/downloads下载service_wrapper-0.1.0-win32.zip,并从bin /中提取service_wrapper.exe。 我将它解压到C:\ service_wrapper。 接下来建立一个配置文件。 我使用了hello示例,并将其修改为我的应用程序,然后将其放在C:\ service_wrapper目录中。 ; ...
-
我的记忆是Rails没有配置为寻找杂种(即使它已经安装)。 你可以直接运行mongrel。 但你可能会考虑转向瘦身。 工作得很好,并自动识别Rails。 http://rubygems.org/gems/thin My memory is that Rails isn't configured to look for mongrel (even if it's installed). You can run mongrel directly though. But you might consider sw ...
-
我已经对Mongrel源进行了一些调查,结果发现Mongrel安装了一个信号处理程序来捕获标准进程kill(TERM)并正常关机,因此我不需要特殊的程序。 您可以在处理请求时查看它在处理Mongrel时获得的日志输出。 例如: ** TERM signal received. Thu Aug 28 00:52:35 +0000 2008: Reaping 2 threads for slow workers because of 'shutdown' Waiting for 2 requests to fi ...
-
在Apache和Mongrel上为Rails应用程序设置虚拟主机(Setting up virtual hosts for Rails apps on Apache and Mongrel)[2024-01-26]
我想到了。 由于我不知道的原因,我需要为RailsApp2设置ServerAlias,并添加“www。”。 在ServerName前面。 所以railsapp2.com vhost文件的顶部现在看起来像这样:ServerName www.railsapp2.com ServerAlias railsapp2.com ... 出于某种原因,RailsApp1不要求这些更改正常运行。 I figured it out. For reasons unknown ... -
我可能只是将所有应用程序移动到mod_rails,因为性能似乎与Mongrel相当,并且管理开销较少。 关于配置问题,只需确保您允许公共目录,否则您将发现静态资产失败:
Options FollowSymLinks AllowOverride None Order allow,deny Allow from all 除此之外,如果你知道如何配置Apache,mod_ ...