- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下查询:
SELECT
Impressions.id AS `Impressions__id`,
Impressions.timestamp AS `Impressions__timestamp`,
Impressions.name AS `Impressions__name`,
Impressions.lat AS `Impressions__lat`,
Impressions.lng AS `Impressions__lng`,
Impressions.personas_count AS `Impressions__personas_count`,
Impressions.modified AS `Impressions__modified`,
Beacons.id AS `Beacons__id`,
Beacons.uuid AS `Beacons__uuid`,
Beacons.major AS `Beacons__major`,
Beacons.minor_dec AS `Beacons__minor_dec`,
Beacons.minor_hex AS `Beacons__minor_hex`,
Beacons.impressions_count AS `Beacons__impressions_count`,
Beacons.created AS `Beacons__created`,
Beacons.modified AS `Beacons__modified`,
Zones.id AS `Zones__id`,
Zones.location_id AS `Zones__location_id`,
Zones.beacon_id AS `Zones__beacon_id`,
Zones.fixture_no AS `Zones__fixture_no`,
Zones.placement AS `Zones__placement`,
Zones.floor AS `Zones__floor`,
Zones.impressions_count AS `Zones__impressions_count`,
Zones.ignore_further_incidents AS `Zones__ignore_further_incidents`,
Zones.is_reviewed AS `Zones__is_reviewed`,
Zones.review_date AS `Zones__review_date`,
Zones.created AS `Zones__created`,
Zones.modified AS `Zones__modified`,
Locations.id AS `Locations__id`,
Locations.retailer_id AS `Locations__retailer_id`,
Locations.google_place_id AS `Locations__google_place_id`,
Locations.regional_name AS `Locations__regional_name`,
Locations.location AS `Locations__location`,
Locations.store_no AS `Locations__store_no`,
Locations.lat AS `Locations__lat`,
Locations.lng AS `Locations__lng`,
Locations.address1 AS `Locations__address1`,
Locations.address2 AS `Locations__address2`,
Locations.address3 AS `Locations__address3`,
Locations.city AS `Locations__city`,
Locations.state AS `Locations__state`,
Locations.postal_code AS `Locations__postal_code`,
Locations.region_id AS `Locations__region_id`,
Locations.country_id AS `Locations__country_id`,
Locations.zones_count AS `Locations__zones_count`,
Locations.contacts_count AS `Locations__contacts_count`,
Locations.created AS `Locations__created`,
Locations.modified AS `Locations__modified`,
Devices.id AS `Devices__id`,
Devices.os AS `Devices__os`,
Devices.bluetooth_enabled AS `Devices__bluetooth_enabled`,
Devices.impressions_count AS `Devices__impressions_count`,
Devices.modified AS `Devices__modified`,
Regions.id AS `Regions__id`,
Regions.country_name AS `Regions__country_name`,
Regions.subdiv AS `Regions__subdiv`,
Regions.subdiv_name AS `Regions__subdiv_name`,
Regions.level_name AS `Regions__level_name`,
Regions.alt_names AS `Regions__alt_names`,
Regions.subdiv_star AS `Regions__subdiv_star`,
Regions.subdiv_id AS `Regions__subdiv_id`,
Regions.country_id AS `Regions__country_id`,
Regions.country_code_2 AS `Regions__country_code_2`,
Regions.country_code_3 AS `Regions__country_code_3`,
Countries.id AS `Countries__id`,
Countries.country_name AS `Countries__country_name`,
Countries.alt_names AS `Countries__alt_names`,
Countries.code2 AS `Countries__code2`,
Countries.code3 AS `Countries__code3`,
Countries.iso_cc AS `Countries__iso_cc`,
Countries.fips_code AS `Countries__fips_code`,
Countries.fips_country_name AS `Countries__fips_country_name`,
Countries.un_region AS `Countries__un_region`,
Countries.un_subregion AS `Countries__un_subregion`,
Countries.comments AS `Countries__comments`,
Countries.created AS `Countries__created`,
Countries.modified AS `Countries__modified`
FROM
impressions Impressions
inner join beacons Beacons ON Beacons.id = (Impressions.beacon_id)
inner JOIN zones Zones ON Zones.id = (Impressions.zone_id)
inner JOIN devices Devices ON Devices.id = (Impressions.device_id)
INNER JOIN locations Locations ON Locations.id = (Zones.location_id)
LEFT JOIN regions Regions ON Regions.id = (Locations.region_id)
LEFT JOIN countries Countries ON Countries.id = (Locations.country_id)
ORDER BY
Impressions.timestamp desc
LIMIT
15 OFFSET 15
此查询大约需要 6 秒才能运行。 EXPLAIN
输出如下:
+----+-------------+-------------+--------+---------------------------------------+----------------+---------+---------------------------------+-------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+--------+---------------------------------------+----------------+---------+---------------------------------+-------+---------------------------------+
| 1 | SIMPLE | Devices | ALL | PRIMARY | NULL | NULL | NULL | 43274 | Using temporary; Using filesort |
| 1 | SIMPLE | Impressions | ref | zone_idx,device_id_idx2,beacon_id_idx | device_id_idx2 | 8 | gen1_d2go.Devices.id | 3 | NULL |
| 1 | SIMPLE | Zones | eq_ref | PRIMARY,fk_location_idx,comp | PRIMARY | 8 | gen1_d2go.Impressions.zone_id | 1 | NULL |
| 1 | SIMPLE | Beacons | eq_ref | PRIMARY | PRIMARY | 8 | gen1_d2go.Impressions.beacon_id | 1 | NULL |
| 1 | SIMPLE | Locations | eq_ref | PRIMARY | PRIMARY | 8 | gen1_d2go.Zones.location_id | 1 | NULL |
| 1 | SIMPLE | Regions | eq_ref | PRIMARY | PRIMARY | 4 | gen1_d2go.Locations.region_id | 1 | NULL |
| 1 | SIMPLE | Countries | eq_ref | PRIMARY | PRIMARY | 4 | gen1_d2go.Locations.country_id | 1 | NULL |
+----+-------------+-------------+--------+---------------------------------------+----------------+---------+---------------------------------+-------+---------------------------------+
7 rows in set (0.00 sec)
我不明白为什么它支持对 Devices
表进行全面扫描。这些表都是索引的,Impressions
和Devices
的CREATE
语句如下:
展示次数
CREATE TABLE `impressions` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`device_id` bigint(20) unsigned NOT NULL,
`beacon_id` bigint(20) unsigned NOT NULL,
`zone_id` bigint(20) unsigned NOT NULL,
`timestamp` datetime NOT NULL,
`google_place_id` bigint(20) unsigned DEFAULT NULL,
`name` varchar(60) DEFAULT NULL,
`lat` decimal(12,7) DEFAULT NULL,
`lng` decimal(12,7) DEFAULT NULL,
`personas_count` int(10) unsigned DEFAULT '0',
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`,`timestamp`),
KEY `zone_idx` (`zone_id`),
KEY `device_id_idx2` (`device_id`),
KEY `beacon_id_idx` (`beacon_id`),
KEY `timestamp_idx` (`id`,`timestamp`),
KEY `ALL` (`id`,`timestamp`,`name`,`lat`,`lng`,`personas_count`,`modified`),
CONSTRAINT `beacon_id` FOREIGN KEY (`beacon_id`) REFERENCES `beacons` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `device2` FOREIGN KEY (`device_id`) REFERENCES `devices` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `zone_FK` FOREIGN KEY (`zone_id`) REFERENCES `zones` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=303907 DEFAULT CHARSET=utf8;
设备
CREATE TABLE `devices` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`device_id` bigint(20) unsigned NOT NULL,
`advertiser_id` char(36) NOT NULL,
`os` varchar(80) DEFAULT NULL,
`bluetooth_enabled` tinyint(1) DEFAULT NULL,
`impressions_count` int(10) unsigned DEFAULT '0',
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `advertiser_idx` (`advertiser_id`),
KEY `ad_dev` (`device_id`,`advertiser_id`),
KEY `device_id` (`device_id`)
) ENGINE=InnoDB AUTO_INCREMENT=53628 DEFAULT CHARSET=utf8;
踢球者是:
当我在 FROM 印象之后使用 FORCE INDEX (timestamp_idx)
时,效果很好。它使用该索引,并在大约 0.078 秒内运行。我不知道为什么它试图避免使用该索引,或者根本不首先从该表中进行选择。
已更新
在 FORCE INDEX 中包含 EXPLAIN
Current database: gen1_d2go
+----+-------------+-------------+--------+------------------------------+---------------+---------+---------------------------------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+--------+------------------------------+---------------+---------+---------------------------------+------+-------+
| 1 | SIMPLE | Impressions | index | NULL | timestamp_idx | 5 | NULL | 15 | NULL |
| 1 | SIMPLE | Zones | eq_ref | PRIMARY,fk_location_idx,comp | PRIMARY | 8 | gen1_d2go.Impressions.zone_id | 1 | NULL |
| 1 | SIMPLE | Beacons | eq_ref | PRIMARY | PRIMARY | 8 | gen1_d2go.Impressions.beacon_id | 1 | NULL |
| 1 | SIMPLE | Locations | eq_ref | PRIMARY | PRIMARY | 8 | gen1_d2go.Zones.location_id | 1 | NULL |
| 1 | SIMPLE | Regions | eq_ref | PRIMARY | PRIMARY | 4 | gen1_d2go.Locations.region_id | 1 | NULL |
| 1 | SIMPLE | Countries | eq_ref | PRIMARY | PRIMARY | 4 | gen1_d2go.Locations.country_id | 1 | NULL |
| 1 | SIMPLE | Devices | eq_ref | PRIMARY | PRIMARY | 8 | gen1_d2go.Impressions.device_id | 1 | NULL |
+----+-------------+-------------+--------+------------------------------+---------------+---------+---------------------------------+------+-------+
7 rows in set (0.01 sec)
最佳答案
Impressions
需要一个索引 starting with timestamp
。这样,优化器将有望决定以 timestamp
顺序扫描 Impressions
,从而避免排序等。
边课...您有 3 个以 id, timestamp
开头的索引。一个是 PRIMARY KEY
。这意味着其他两个是不必要的。
因此可以获得额外的加速:
ALTER TABLE Impressions
DROP INDEX timestamp_idx, -- as already mentioned
DROP INDEX ALL, -- ditto
DROP PRIMARY KEY, -- to rearrange it
ADD PRIMARY KEY(timestamp, id), -- thus
ADD INDEX(id); -- and keep AUTO_INCREMENT happy
为什么?通过使 PK 以时间戳开始,查询可以扫描数据而不是在某些索引和数据之间跳转。这将加快相关查询的速度。警告:它可能会伤害其他查询。
其他注意事项...
CHAR(36)
闻起来像 UUID,对吗?但是对于 utf8,它恰好需要 108 个字节!。更改为 CHAR(36) CHARACTER SET ascii NOT NULL
,这样它就只需要 36 个字节。 (或者您可以转换为 `BINARY(16) 以节省更多;但那是另一回事,需要更多代码。)
除非您有数十亿行,否则 BIGINT
(8 字节)对于 id 来说太过分了。 INT UNSIGNED
只有 4 个字节。
更小以各种方式转化为更快。
关于MySQL Select with several join 首先对连接表进行全表扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35921916/
在 的 React 组件中菜单,我需要设置selected反射(reflect)应用程序状态的选项的属性。 在 render() , optionState从状态所有者传递给 SortMenu 组件
我是初级 Ruby-mysql 程序员,我想知道如何使我的(存储过程)查询结果更快.. 这是我的存储过程我正在使用 SQL_CACHE.. 但我不确定.. 缓存使我的过程更快.. : ( DROP
我一直在 Python 中进行套接字编程,以使用 select.select(rfile, wfile, xlist[, timeout]) 处理由已连接的客户端套接字列表发出的请求,并且我还想用 J
我试图通过用空格填充文本来创建下拉列表中的列效果,如下例所示: [Aux1+1] [*] [Aux1+1] [@Tn=PP] [Main] [*] [Main A
我为 MySQL 编写了以下查询: SELECT subquery.t1_column1, subquery.t2_id, MAX(subquery.val) FROM ( S
为什么我们要用 select 标签来编写.attr('selected','selected') 例如: $('#countryList option').filter(function () {
Lokalizacja: Gdańsk Rzeszów Wrocław 不知道发生了什么,但在那种情况下没有选择的选项,我必须从列表中选择一些东西。当我从选
我的表单中有两个选择字段。第一个是单选,另一个是多选。现在我想做的是根据单选中所选的选项,使用给定的数据选择多选中的选项。为此,我在单选更改时触发 ajax 请求: $.ajax({ type
我在 Firefox 5 中发现了一个奇怪的错误(我现在无法访问 4)。但是,我认为它可能在 Firefox 4 中工作,因为我刚买了一台新电脑,而且我不记得以前见过这个错误。 我有几个选择框。所选值
此 SQL 有何不同: 第一个: select * from table_1 a join table_2 b on a.id = b.acc_id 第二个: select * f
预选 的最佳做法是什么?在 ? 根据不同的网站,两者都有效。但是哪个更好呢?最兼容? Foo Bar 最佳答案 如果您正在编写 XHTML,则 selected="selected" 是必需的。 如
我使用 Angular JS 创建了一个多选选择框:下面是相同的代码: JS: $scope.foobars = [{ 'foobar_id': 'foobar01', 'name':
我在 jqGrid 中有几列 edittype="select"。如何读取特定行中当前选定值的选项值? 例如:当我提供以下选项时,如何获得 FedEx 等的“FE” editoption: { val
这是我更大问题的精简查询,但要点是我试图内部联接到一个选择,其中选择受到外部选择的限制。那可能吗?我在内部选择上收到有关多部分标识符 S.Item 和 S.SerialNum 的错误。 要点是这样的,
如果chat.chat_type IS NULL,我想选择user.*,但如果chat.chat_type = 1 我想选择组。* SELECT CASE WHEN ch
我正在编写一个小脚本来测试表单在提交之前是否已被更改。所以我可以使用普通输入(文本、文本区域等): if(element.defaultValue != element.value) { al
我正在尝试为 Prototype 编写一个插件,用户在其中单击下拉菜单并将其替换为多选元素。我快完成了。在用户选择他们想要显示的内容并将表单提交到同一页面之前,一切都很好。我正在使用 PHP 来使用
你如何在 MongoDB 中进行嵌套选择,类似于 SELECT id FROM table1 WHERE id IN (SELECT id FROM table2) 最佳答案 MongoDB 尚不具备
我有以下用于选择下拉列表的代码: {{unit.Text}} UnitOfMeasurements 数组中的每一项看起来像这样: Selected: false Text: "lb" Va
我正在尝试使用[选定]和[ngValue]来设置表单中包含对象的选择标记的默认值。但出于某种原因,它们似乎无法相提并论。。示例代码:。这段代码最终只显示空白作为缺省值。如果删除[ngValue],它就
我是一名优秀的程序员,十分优秀!