我有一个测试表 (table1),其中有 6 条记录。我想根据列 (col1) 获取多个值的数据。 所以我索引了该列。现在,如果我在 IN 子句中传递多个值,选择具有强制索引的所有列 (*),我将获得特定记录而不是全表扫描。如果我对选定的列运行相同的查询,我会看到它执行全表扫描。
我读到在 select 查询中使用 select all (*) 是不好的。但是这里如果我不使用全选 (*) 将会进行全表扫描。 我无法理解 mysql 如何读取查询。请帮我解决这个问题。
表格
+----+--------+---------+
| id | col1 | col2 |
+----+--------+---------+
| 1 | 100000 | E100000 |
| 2 | 100001 | E200001 |
| 3 | 100002 | E300002 |
| 4 | 100003 | E400003 |
| 5 | 100004 | E500004 |
| 6 | 100005 | E600005 |
+----+--------+---------+
索引
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| table1 | 0 | PRIMARY | 1 | id | A | 6 | NULL | NULL | | BTREE | | |
| table1 | 1 | col1 | 1 | col1 | A | 6 | NULL | NULL | | BTREE | | |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
EXPLAIN(使用 FORCE INDEX (col1) 并选择所有 (*) 列)
select * from table1 force index(col1) where col1 in ('100000', '100001');
+----+-------------+----------+-------+---------------+-------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------+---------------+-------+---------+------+------+-------------+
| 1 | SIMPLE | table1 | range | col1 | col1 | 10 | NULL | 2 | Using where |
+----+-------------+----------+-------+---------------+-------+---------+------+------+-------------+
EXPLAIN(使用 FORCE INDEX (col1) 并且只选择 1 列数据而不是全部(*))
select col1 from table1 force index(col1) where col1 in ('100000', '100001');
+----+-------------+----------+-------+---------------+-------+---------+------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------+---------------+-------+---------+------+------+--------------------------+
| 1 | SIMPLE | table1 | range | col1 | col1 | 10 | NULL | 6 | Using where; Using index |
+----+-------------+----------+-------+---------------+-------+---------+------+------+--------------------------+
最佳答案
优化器如何确定什么更有效? 它试图预测磁盘读取 block 操作的数量。
正如之前在评论中提到的,在大表上 EXPLAIN 会有所不同。
关于Mysql IN子句全表扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19141365/
这段代码没有像我预期的那样执行:casewhen->{false}then"why?"else"ThisiswhatIexpect"end#=>"why?"这也不是casewhen->(x){false}then"why?"else"ThisiswhatIexpect"end#=>"why?"第一个then子句在两种情况下都被执行,这意味着我提供给when子句的lambda没有被调用。我知道无论when子句的主题是什么,都应该调用大小写相等运算符===。我想知道当没有为case提供参数时,===的另一边会发生什么。我在想它可能是nil,但它不可能是:->{false}===nil#=>
可能真的很简单,但我很难在网上找到关于这个的文档我在Ruby中有两个activerecord查询,我想通过OR运算符连接在一起@pro=Project.where(:manager_user_id=>current_user.id)@proa=Project.where(:account_manager=>current_user.id)我是ruby的新手,但我自己尝试使用||@pro=Project.where(:manager_user_id=>current_user.id||:account_manager=>current_user.id)这没有用,所以1.我想知道如何在
我想扫描未知数量的行,直到扫描完所有行。我如何在ruby中做到这一点?例如:putreturnsbetweenparagraphsforlinebreakadd2spacesatend_italic_or**bold**输入不是来自"file",而是通过STDIN。 最佳答案 在ruby中有很多方法可以做到这一点。大多数情况下,您希望一次处理一行,例如,您可以使用whileline=getsend或STDIN.each_linedo|line|end或者通过使用-n开关运行ruby,例如,这意味着上述循环之一(在每次迭代中将
我有一个看起来像这样的类:classFoo在测试#nasty_bars_present?我想编写一个rspec测试来对bars关联进行stub,但允许where自然执行。像这样的东西:describe"#nasty_bars_present?"docontext"withnastybars"dobefore{foo.stub(:bars).and_return([mock(Bar,bar_type:"Nasty")])}it"shouldreturntrue"doexpect(foo.nasty_bars_present?).tobe_trueendendend上面的测试给出了一个关于
如何扫描上传文件中的病毒、木马等?只是想阻止一些用户上传一些讨厌的东西。我正在使用Heroku和AmazonS3。 最佳答案 Checkoutthis它支持REST/JSON防病毒网络服务。 关于ruby-on-rails-Rails/Heroku-如何对上传的文件进行反病毒扫描?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/9640516/
考虑以下代码片段:defsqlbilling_requests.project(billing_requests[Arel.star]).where(filter_by_day.and(filter_by_merchant).and(filter_by_operator_name)).to_sqlenddeffilter_by_daybilling_requests[:created_at].gteq(@start_date).and(billing_requests[:created_at].lteq(@end_date))enddeffilter_by_operator_nameu
根据AWSDocs:Anupdateexpressionconsistsofoneormoreclauses.EachclausebeginswithaSET,REMOVE,ADDorDELETEkeyword.Youcanincludeanyoftheseclausesinanupdateexpression,inanyorder.However,eachactionkeywordcanappearonlyonce.我无法在一个update_expression中获得正确的SET和REMOVE语法:params={key:{'id'=>{s:'123'}},table_name:'c
Ruby(1.9.3)文档似乎暗示scan等同于=~除了scan返回多个匹配项,而=~仅返回第一个匹配项,并且scan返回匹配数据,而=~返回索引。但是,在下面的示例中,这两种方法似乎对相同的字符串和表达式返回不同的结果。这是为什么?1.9.3p0:002>str="PerlandPython-thetwolanguages"=>"PerlandPython-thetwolanguages"1.9.3p0:008>exp=/P(erl|ython)/=>/P(erl|ython)/1.9.3p0:009>str=~exp=>01.9.3p0:010>str.scanexp=>[["er
我有一段代码,其中有一个带有保护子句的raise语句:defvalidate_indexindex#ChangetoSizeErrorraiseArgumentError,"Sizeofindex(#{index.size})doesnotmatches"\"sizeofvector(#{size})"ifsize!=index.sizeend在这一点上,rubocop给出了罪行:Style/MultilineIfModifier:Favoranormalif-statementoveramodifierclauseinamultilinestatement.我将我的代码修改为正常if
我正在使用RubyonRails3.2.2,我想生成以下SQL查询:SELECT`articles`.*FROM`articles`WHERE(`articles`.`user_id`=1OR`articles`.`status`='published'OR(`articles`.`status`='temp'AND`articles`.`user_id`IN(10,11,12,)))通过使用Arel这样Article.where(arel_table[:user_id].eq(1).or(arel_table[:status].eq("published")).or(arel_tab