- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
查看 various answers for ORDER BY with CASE like this one ,我看到我在这个遗留应用程序中被迫做的很可能是专家方法;但是,当行数不多时(100,000 行或更多行导致页面加载时间为 10 秒),它就太慢了。
请注意,原始查询旨在解决一个明显常见的问题,即查询分析师需要空排序的日期,而不是通常的排序方式。在这种情况下,datefirstprinted
将降序排列,但所有未打印的记录都应填充到列表的顶部。
Original Query 解决了这个问题,但问题的重点是避免派生列 notprintedyet
带来的 filesort
性能损失.
Original Query
SELECT SQL_NO_CACHE
id, daterun, datefirstprinted,
case datefirstprinted when "0000-00-00 00:00:00" then 1 else 0 end as notprintedyet
FROM
patientrecords
WHERE
dateuploaded <> '0000-00-00 00:00:00'
ORDER BY
notprintedyet desc, /* ordered via alias */
datefirstprinted desc
LIMIT 10;
时间1.52s
我发现不对别名 notprintedyet
进行排序可以节省一点:
Slightly Faster Query
SELECT SQL_NO_CACHE
id, daterun, datefirstprinted,
case datefirstprinted when "0000-00-00 00:00:00" then 1 else 0 end as notprintedyet
FROM
patientrecords
WHERE
dateuploaded <> '0000-00-00 00:00:00'
ORDER BY
datefirstprinted = "0000-00-00 00:00:00" desc, /* directly ordered */
datefirstprinted
LIMIT 10;
时间1.37s
Optimal Speed, but missing required sorting of empty dates first
SELECT SQL_NO_CACHE
id, daterun, datefirstprinted,
case datefirstprinted when "0000-00-00 00:00:00" then 1 else 0 end as notprintedyet
FROM
patientrecords
WHERE
dateuploaded <> '0000-00-00 00:00:00'
ORDER BY
datefirstprinted /* not ordered properly */
LIMIT 10;
时间0.48s
I tried using a view
create view notprinted_patientrecords as (
SELECT id, daterun, datefirstprinted, case datefirstprinted when "0000-00-00 00:00:00" then 1 else 0 end notprintedyet
FROM patientrecords
WHERE dateuploaded <> '0000-00-00 00:00:00'
);
不幸的是当我运行 explain
explain select * from notprinted_patientrecords order by notprintedyet desc limit 10;
它表明我仍在使用 filesort
并花费了 1.51s 也就是完全没有节省
Would it be faster if datefirstprinted default is NULL?
也许吧,但在这个旧版应用中,这可能比页面加载时间额外增加 5 秒造成的危害更大
What else might we try? Stored procedures? Functions?
更新
As suggested @strawberry - ORDER BY CASE
...
ORDER BY
case datefirstprinted when "0000-00-00 00:00:00" then 1 else 0 end, datefirstprinted
LIMIT 10;
时间1.52s
根据@e4c5 的要求,explain
输出:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: patientrecords
type: range
possible_keys: dateuploaded,uploads_report
key: dateuploaded
key_len: 5
ref: NULL
rows: 299095
Extra: Using index condition; Using filesort
未正确排序除外,它具有以下差异
rows: 10
Extra: Using where
建表语句
*************************** 1. row ***************************
Table: patientrecords
Create Table: CREATE TABLE `patientrecords` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`datecreated` datetime NOT NULL,
`dateuploaded` datetime NOT NULL,
`daterun` datetime NOT NULL,
`datebilled` datetime NOT NULL,
`datefirstprinted` datetime NOT NULL,
`datelastprinted` datetime NOT NULL,
`client` varchar(5) NOT NULL,
PRIMARY KEY (`id`),
KEY `dateuploaded` (`dateuploaded`),
KEY `daterun` (`daterun`),
KEY `uploads_report` (`dateuploaded`,`client`),
KEY `datefirstprinted` (`datefirstprinted`),
KEY `datelastprinted` (`datelastprinted`)
)
最佳答案
看你的表,首先要注意的是下面的索引是多余的
KEY `dateuploaded` (`dateuploaded`),
它的作用可以由这个来完成
KEY `uploads_report` (`dateuploaded`,`client`),
所以让我们删除 dateuploaded
键。不清楚您是否在任何查询中实际使用了客户列。如果你不这样做,我相信按如下方式更改你的索引会给你一个很大的加速
KEY `uploads_report` (`dateuploaded`,`datefirstprinted`,`client`),
这是因为mysql每张表只能使用一个索引。由于 dateuploaded 列上的索引正在 where 子句中使用,因此不能使用 datefirstprinted
的索引。但是,如果您将这两列合并到同一个索引中,它就可以同时用于排序和位置。
在你做了上面的索引之后,这个可能会被删除:
KEY `datefirstprinted` (`datefirstprinted`),
索引越少,插入和更新越快。
关于mysql ORDER BY with CASE - 太慢,更快的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41688824/
好吧,我知道这个问题已经被问了无数次了。但是,对于我在谷歌搜索中似乎无法找到的问题,我还有一个小补充。 我当然不是 FFMPEG 的专家……我一直在使用 FFMPEG 的标准加速/减速模板,我正在使用
考虑这三个文档... [ { _id: "...", _rev: "...", title: "Foo", body: "...
我想知道访问我的全局变量的最快方法...它们只会在 Beta 测试阶段发生变化。在我们上线之前。从那时起,它们将永远不会改变。 我认为从 web.config 中获取内容会产生开销,而且编写 App.
这个问题在这里已经有了答案: 11 年前关闭。 Possible Duplicate: Is there a performance difference between BETWEEN and IN
我很想知道对通常作为查询目标的数字列进行分区是否有性能优势。目前我有一个包含约 5000 万条记录的物化 View 。当使用常规 b 树索引并按此数字列搜索时,我得到的成本为 7,查询结果大约需要 0
我需要编写一个库,它执行许多远程 HTTP 调用来获取内容。我可以按照描述做here ,但是有没有更好的方法(在性能方面)如何做到这一点?如果我按照示例中所述进行操作,我总是会创建一个 URL 对象,
该代码非常不言自明。只是有很多我需要独立随机化的范围。例如,范围('W1:W4')不应与范围('W5:W8')混淆,因此我不能只是随机化范围('W1:W80')。任何帮助或建议都会很棒!多谢。目前,代
我正在使用 ADT 模拟器。我在我的模拟器中使用默认的 Android 虚拟设备。我创建了一个版本 4.0.3。 问题 太慢了。有时我在尝试更改 fragment 时会收到加载点击。 我使用的代码是有
我正在尝试获取一个包含三个表中的信息的数组。结果应该是一个数组,我可以在其中循环遍历第一个表、第二个表中的相关行以及第三个表到第二个表中的相关行。目前,我有三个独立的 SQL 查询,然后将它们重组为一
我已经学会了两种在服务器上上传图像的方法(可能还有更多..)。 1) 创建 NSData 并将其添加到请求正文中 2)创建字节数组并像简单数组一样以json形式发送 1) 创建 NSData 并将其添
我有一个 UItextview,我可以在里面写入数据类,我可以在我的 View 中的任何地方提供数据,在 ViewDidAppear 函数中我传递了我的数据,但它有点慢。文本在 0.2-0.3 秒后出
如何为 discoverAllContactUserInfosWithCompletionHandler 创建优先级高于默认值的 CKOperation? 我找不到不使用 [[CKContainer
我在 unix 模块下编写了一个内核级函数,用于对系统负载进行采样。我在 clock.c 下的 clock() 中调用示例函数,以在每个时钟(例如,我的系统上每 10 毫秒)拍摄系统负载的快照。有没有
我正在制作一个应用程序,该应用程序将根据变量的值使用鼠标/键盘(宏)模拟操作。 这里有我制作的 de 扫描代码: void ReadMemory(int value){ DWORD p
我想知道在计算上调用嵌套在对象中的函数的最快方法是什么,所以我做了一个快速的 jsPerf.com 基准测试,其中我考虑了三种可能性——从数组中调用函数,从“核心”中调用函数对象和函数对象: var
我用 php 做了一个图像缩放器。调整图像大小时,它会缓存一个具有新尺寸的新 jpg 文件。下次您调用确切的 img.php?file=hello.jpg&size=400 时,它会检查是否已经创建了
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Which is best for data store Struct/Classes? 考虑我有一个 Em
我正在尝试为多组列自动计算每行的平均分数。例如。一组列可以代表不同比例的项目。这些列也被系统地命名 (scale_itemnumber)。 例如,下面的虚拟数据框包含来自三个不同比例的项目。(可能会出
所以我知道散列图使用桶和散列码等等。根据我的经验,Java 哈希码并不小,但通常很大,所以我假设它没有在内部建立索引。除非哈希码质量很差导致桶长度和桶数量大致相等,否则 HashMap 比名称-> 值
假设我有一个非常缓慢和大的 for 循环。 如何将其拆分为多个线程以使其运行速度更快? for (int a = 0; a { slowMet
我是一名优秀的程序员,十分优秀!