jjzjj

php - : <derived2> Using temporary; Using filesort 查询非常慢

coder 2023-10-17 原文

我的选择类别查询有性能问题 Restructuring a DB for best performance 所以我设定了一个目标来解决它。但最后发现它是一个更复杂的查询,与原始查询相比,性能实际上有所下降。

SELECT *
FROM   post
       LEFT JOIN post_plus
         ON ( post.id = post_plus.news_id )
       INNER JOIN (SELECT DISTINCT c1.postid
                  FROM   post_category c1
                         JOIN post_category c2
                           ON c1.postid = c2.postid
                  WHERE  c1.categoryid IN ( 130, 3, 4, 5 )
                         AND c2.categoryid = 73) post_category 
         ON ( post_category.postid = post.id ) 
WHERE  approve = 1
ORDER  BY fixed DESC,
          date DESC     
LIMIT  0, 7;           

新查询需要:(1.02 秒)- 使用正则表达式的旧查询花费了(0.23 秒)

我只能猜测是因为 Using temporary;使用文件排序

我怎样才能摆脱这个?

解释查询:

`+----+-------------+---------------+--------+-------------------+------------+---------+--------------------------+-------+----------------------------------------+
| id | select_type | table         | type   | possible_keys     | key        | key_len | ref                      | rows  | Extra                                  |
+----+-------------+---------------+--------+-------------------+------------+---------+--------------------------+-------+----------------------------------------+
|  1 | PRIMARY     | <derived2>    | ALL    | NULL              | NULL       | NULL    | NULL                     | 10470 | Using temporary; Using filesort        |
|  1 | PRIMARY     | post      | eq_ref | PRIMARY,approve   | PRIMARY    | 4       | post_category.postid |     1 | Using where                            |
|  1 | PRIMARY     | post_plus | ref    | news_id           | news_id    | 5       | post_category.postid |     1 | NULL                                   |
|  2 | DERIVED     | c1            | range  | postId,categoryId | categoryId | 2       | NULL                     | 10470 | Using index condition; Using temporary |
|  2 | DERIVED     | c2            | ref    | postId            | postId     | 4       | online_test.c1.postId    |     1 | Using index                            |
+----+-------------+---------------+--------+-------------------+------------+---------+--------------------------+-------+----------------------------------------+

`

张贴表格

| post | CREATE TABLE `post` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `autor` varchar(40) NOT NULL DEFAULT '',
  `date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `short_story` text NOT NULL,
  `full_story` text NOT NULL,
  `xfields` text NOT NULL,
  `title` varchar(255) NOT NULL DEFAULT '',
  `descr` varchar(200) NOT NULL DEFAULT '',
  `keywords` text NOT NULL,
  `category` varchar(200) NOT NULL DEFAULT '0',
  `alt_name` varchar(200) NOT NULL DEFAULT '',
  `comm_num` mediumint(8) unsigned NOT NULL DEFAULT '0',
  `allow_comm` tinyint(1) NOT NULL DEFAULT '1',
  `allow_main` tinyint(1) unsigned NOT NULL DEFAULT '1',
  `approve` tinyint(1) NOT NULL DEFAULT '0',
  `fixed` tinyint(1) NOT NULL DEFAULT '0',
  `allow_br` tinyint(1) NOT NULL DEFAULT '1',
  `symbol` varchar(3) NOT NULL DEFAULT '',
  `tags` varchar(255) NOT NULL DEFAULT '',
  `metatitle` varchar(255) NOT NULL DEFAULT '',
  `FileTempUUID` varchar(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `autor` (`autor`),
  KEY `alt_name` (`alt_name`),
  KEY `category` (`category`),
  KEY `allow_main` (`allow_main`),
  KEY `date` (`date`),
  KEY `symbol` (`symbol`),
  KEY `comm_num` (`comm_num`),
  KEY `tags` (`tags`),
  KEY `descr` (`descr`),
  KEY `title` (`title`),
  KEY `approve` (`approve`,`allow_main`),
  FULLTEXT KEY `full_story` (`full_story`),
  FULLTEXT KEY `short_story` (`short_story`,`full_story`,`xfields`,`title`),
  FULLTEXT KEY `title_2` (`title`)
) ENGINE=InnoDB AUTO_INCREMENT=32586 DEFAULT CHARSET=utf8 |

post_plus

| post_plus | CREATE TABLE `post_plus` (
  `pid` int(11) NOT NULL AUTO_INCREMENT,
  `news_id` int(11) DEFAULT NULL,
  `kp_id` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
  `pdate` datetime DEFAULT NULL,
  `news_read` int(11) NOT NULL DEFAULT '0',
  `user_id` int(11) NOT NULL DEFAULT '0',
  `allow_rate` tinyint(1) NOT NULL DEFAULT '1',
  `rating` mediumint(8) NOT NULL DEFAULT '0',
  `vote_num` mediumint(8) NOT NULL DEFAULT '0',
  `votes` tinyint(1) NOT NULL DEFAULT '0',
  `editdate` int(11) DEFAULT NULL,
  `view_edit` tinyint(1) NOT NULL DEFAULT '0',
  `editor` varchar(40) CHARACTER SET utf8 NOT NULL DEFAULT '',
  `reason` varchar(255) CHARACTER SET utf8 NOT NULL DEFAULT '',
  `access` varchar(150) CHARACTER SET utf8 NOT NULL DEFAULT '',
  PRIMARY KEY (`pid`),
  KEY `user_id` (`user_id`),
  KEY `news_id` (`news_id`)
) ENGINE=InnoDB AUTO_INCREMENT=32819 DEFAULT CHARSET=latin1 |

post_category 表

| post_category | CREATE TABLE `post_category` (
  `cid` bigint(11) NOT NULL AUTO_INCREMENT,
  `postId` int(11) NOT NULL,
  `categoryId` smallint(6) NOT NULL,
  PRIMARY KEY (`cid`),
  KEY `postId` (`postId`,`categoryId`),
  KEY `categoryId` (`categoryId`)
) ENGINE=InnoDB AUTO_INCREMENT=100870 DEFAULT CHARSET=latin1 |

最佳答案

我认为这个查询在精神上是等价的,并且可能执行得更快。特别是如果您在 post_category (postid, categoryid)

上创建索引
Select
    *
From
    post
        Left Join
    post_plus
        On post.id = post_plus.news_id
Where
    Exists (
        Select
            'x'
        From
            post_category c1
        Where
            categoryid = 73 and
            c1.postid = post.postid
    ) And
    Exists (
        Select
            'x'
        From
            post_category c2
        Where
            categoryid In (130, 3, 4, 5) and
            c2.postid = post.postid
    ) And
    approve = 1
Order By
    fixed Desc,
    date Desc
Limit
    0, 7;

由于postid, categoryidpost_category上是唯一的,你也可以这样改写。它可能会受益于相反顺序的索引 (categoryid, postid):

Select
    post.*,
    post_plus.*
From
    post_category c1
        Inner Join
    post
        On c1.categoryid = 73 and c1.postid = post.postid
        Left Join
    post_plus
        On post.id = post_plus.news_id
Where
    Exists (
        Select
            'x'
        From
            post_category c2
        Where
            categoryid In (130, 3, 4, 5) and
            c2.postid = post.postid
    ) And
    approve = 1
Order By
    fixed Desc,
    date Desc
Limit
    0, 7;

关于php - : <derived2> Using temporary; Using filesort 查询非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18689011/

有关php - : <derived2> Using temporary; Using filesort 查询非常慢的更多相关文章

  1. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  2. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  3. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  4. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

  5. ruby-on-rails - Nokogiri:使用 XPath 搜索 <div> - 2

    我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll

  6. sql - 查询忽略时间戳日期的时间范围 - 2

    我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时

  7. ruby-on-rails - 使用 HTTParty 的非常基本的 Rails 4.1 API 调用 - 2

    Rails相对较新。我正在尝试调用一个API,它应该向我返回一个唯一的URL。我的应用程序中捆绑了HTTParty。我已经创建了一个UniqueNumberController,并且我已经阅读了几个HTTParty指南,直到我想要什么,但也许我只是有点迷路,真的不知道该怎么做。基本上,我需要做的就是调用API,获取它返回的URL,然后将该URL插入到用户的数据库中。谁能给我指出正确的方向或与我分享一些代码? 最佳答案 假设API为JSON格式并返回如下数据:{"url":"http://example.com/unique-url"

  8. ruby-on-rails - 没有参数的 `<<`(小于两倍)是什么意思? - 2

    我在一个我想在formtasticGem中覆盖的方法中找到了这个。该方法如下所示:defto_htmlinput_wrappingdohidden_field_html是什么意思?在第三行做什么?我知道它对数组有什么作用,但在这里我不知道。 最佳答案 你可以这样读:hidden_field_htmllabel_with_nested_checkbox是连接到hidden_​​field_html末尾的参数-为了“清晰”,他们将其分成两行 关于ruby-on-rails-没有参数的`

  9. ruby-on-rails - 找不到 gem railties (>= 0.a) (Gem::GemNotFoundException) - 2

    我已经看到了一些其他的问题,尝试了他们的建议,但没有一个对我有用。我已经使用Rails大约一年了,刚刚开始一个新的Rails项目,突然遇到了问题。我卸载并尝试重新安装所有Ruby和Rails。Ruby很好,但Rails不行。当我输入railss时,我得到了can'tfindgemrailties。我当前的Ruby版本是ruby2.2.2p95(2015-04-13修订版50295)[x86_64-darwin15],尽管我一直在尝试通过rbenv设置ruby​​2.3.0。如果我尝试rails-v查看我正在运行的版本,我会得到同样的错误。我使用的是MacOSXElCapitan版本10

  10. ruby-on-rails - 连接字符串时如何在 <%=%> block 内输出 html_safe? - 2

    考虑一下:现在这些情况:#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2我需要用其他字符串输出URL。我如何保证&符号不会被转义?由于我无法控制的原因,我无法发送&。求助!把我的头发拉到这里:\编辑:为了澄清,我实际上有一个像这样的数组:@images=[{:id=>"fooid",:url=>"http://

随机推荐