- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我遇到了 Postgres 的性能问题。我有两个具有相同结构、相同索引的表,我还在两个表的 id_coordinate 索引上执行了相同的 CLUSTER。这些表具有以下结构:
Column | Type | Modifiers | Storage | Description
----------------+----------+-------------------------------------------+---------+-------------
id_best_server | integer | not null default nextval('seq'::regclass) | plain |
date | date | not null | plain |
id_coordinate | integer | not null | plain |
mnc | smallint | | plain |
id_cell | integer | | plain |
rx_level | real | | plain |
rx_quality | real | | plain |
sqi | real | | plain |
Indexes:
"history_best_server_until_2013_10_pkey" PRIMARY KEY, btree (id_best_server)
"ix_history_best_server_until_2013_10_id_coordinate" btree (id_coordinate) CLUSTER
"ix_history_best_server_until_2013_10_id_best_server" btree (id_best_server)
执行的查询:
EXPLAIN ANALYZE SELECT DISTINCT ON (x, y) x, y, rx_level, rx_quality, date, mnc, id_cell
FROM
(
SELECT X(co.location) AS x, Y(co.location) AS y, tems.rx_level, tems.rx_quality, date, mnc, id_cell
FROM tems.history_best_server_until_2012_10 AS tems
JOIN gis.coordinate AS co ON tems.id_coordinate = co.id_coordinate
AND co.location && setsrid(makeBox2d(GeomFromText('POINT(101000 461500)', 2710),
GeomFromText('POINT(102400 463610)', 2710)
), 2710)
WHERE mnc = 41
) AS j1
ORDER BY x, y, date DESC
两个表的行数几乎相同(大约 8M)。当我执行上面的查询时,在一张表上我得到了这些结果:
"Unique (cost=245742.87..245805.99 rows=8416 width=118) (actual time=3420.966..3425.584 rows=10009 loops=1)"
" -> Sort (cost=245742.87..245763.91 rows=8416 width=118) (actual time=3420.963..3422.236 rows=10212 loops=1)"
" Sort Key: (x(co.location)), (y(co.location)), tems.date"
" Sort Method: quicksort Memory: 1182kB"
" -> Hash Join (cost=61069.15..245194.20 rows=8416 width=118) (actual time=191.365..3405.590 rows=10212 loops=1)"
" Hash Cond: (tems.id_coordinate = co.id_coordinate)"
" -> Seq Scan on history_best_server_until_2012_10 tems (cost=0.00..147705.35 rows=3226085 width=22) (actual time=0.009..1749.468 rows=3230507 loops=1)"
" Filter: (mnc = 41)"
" -> Hash (cost=60697.73..60697.73 rows=29714 width=104) (actual time=46.828..46.828 rows=31806 loops=1)"
" Buckets: 4096 Batches: 1 Memory Usage: 1864kB"
" -> Bitmap Heap Scan on coordinate co (cost=937.22..60697.73 rows=29714 width=104) (actual time=14.975..35.561 rows=31806 loops=1)"
" Recheck Cond: (location && '0103000020960A000001000000050000000000000080A8F84000000000F02A1C410000000080A8F84000000000E84B1C41000000000000F94000000000E84B1C41000000000000F94000000000F02A1C410000000080A8F84000000000F02A1C41'::geome (...)"
" -> Bitmap Index Scan on ix_coordinate_location (cost=0.00..929.79 rows=29714 width=0) (actual time=14.593..14.593 rows=31806 loops=1)"
" Index Cond: (location && '0103000020960A000001000000050000000000000080A8F84000000000F02A1C410000000080A8F84000000000E84B1C41000000000000F94000000000E84B1C41000000000000F94000000000F02A1C410000000080A8F84000000000F02A1C41'::g (...)"
"Total runtime: 3426.635 ms"
在另一张 table 上,它看起来像这样:
"Unique (cost=267070.35..267138.75 rows=9120 width=118) (actual time=172.333..177.232 rows=10051 loops=1)"
" -> Sort (cost=267070.35..267093.15 rows=9120 width=118) (actual time=172.330..173.708 rows=10256 loops=1)"
" Sort Key: (x(co.location)), (y(co.location)), tems.date"
" Sort Method: quicksort Memory: 1186kB"
" -> Nested Loop (cost=937.22..266470.49 rows=9120 width=118) (actual time=14.876..156.322 rows=10256 loops=1)"
" -> Bitmap Heap Scan on coordinate co (cost=937.22..60697.73 rows=29714 width=104) (actual time=14.788..29.510 rows=31806 loops=1)"
" Recheck Cond: (location && '0103000020960A000001000000050000000000000080A8F84000000000F02A1C410000000080A8F84000000000E84B1C41000000000000F94000000000E84B1C41000000000000F94000000000F02A1C410000000080A8F84000000000F02A1C41'::geometry)"
" -> Bitmap Index Scan on ix_coordinate_location (cost=0.00..929.79 rows=29714 width=0) (actual time=14.409..14.409 rows=31806 loops=1)"
" Index Cond: (location && '0103000020960A000001000000050000000000000080A8F84000000000F02A1C410000000080A8F84000000000E84B1C41000000000000F94000000000E84B1C41000000000000F94000000000F02A1C410000000080A8F84000000000F02A1C41'::geometr (...)"
" -> Index Scan using ix_history_best_server_until_2013_10_id_coordinate on history_best_server_until_2013_10 tems (cost=0.00..6.91 rows=1 width=22) (actual time=0.003..0.003 rows=0 loops=31806)"
" Index Cond: (id_coordinate = co.id_coordinate)"
" Filter: (mnc = 41)"
"Total runtime: 178.280 ms"
总运行时间不同。
如果不使用“WHERE mnc = 41”,它们都工作得很快。我不知道是什么导致了第一种情况下的序列扫描。请注意,mnc 只能具有 3 个可能值之一。每个值的频率在较快的表上约为 41%、39%、20%,在较慢的表上约为 43%、41%、16%。
添加:这是快速表的统计信息。
tablename | attname | n_distinct | correlation | most_common_freqs
-----------------------------------+----------------+------------+-------------+-------------------
history_best_server_until_2013_10 | id_best_server | -1 | 1 |
history_best_server_until_2013_10 | date | 1122 | -0.206991 | many values
history_best_server_until_2013_10 | id_coordinate | -0.373645 | 1 | many values
history_best_server_until_2013_10 | mnc | 3 | 0.30477 | {0.411783,0.386967,0.20125}
history_best_server_until_2013_10 | id_cell | 5811 | -0.0759416 | many values
history_best_server_until_2013_10 | rx_level | 14961 | -0.122292 | many values
history_best_server_until_2013_10 | rx_quality | 16 | 0.360472 | many values
history_best_server_until_2013_10 | sqi | 5552 | 0.212023 | many values
(8 rows)
这个是慢的:
tablename | attname | n_distinct | correlation | most_common_freqs
-----------------------------------+----------------+------------+-------------+-------------------
history_best_server_until_2012_10 | id_best_server | -1 | 1 |
history_best_server_until_2012_10 | date | 954 | -0.205897 | many values
history_best_server_until_2012_10 | id_coordinate | -0.421911 | 1 | many values
history_best_server_until_2012_10 | mnc | 3 | 0.314319 | {0.4349,0.402433,0.162667}
history_best_server_until_2012_10 | id_cell | 5617 | -0.0715787 | many values
history_best_server_until_2012_10 | rx_level | 14129 | -0.115288 | many values
history_best_server_until_2012_10 | rx_quality | 22 | 0.368943 | many values
history_best_server_until_2012_10 | sqi | 5320 | 0.226596 | many values
gis.coordinate 的表定义
Table "gis.coordinate"
Column | Type | Modifiers | Storage | Description
---------------+----------+------------------------------------------------------------------------+---------+-------------
id_coordinate | integer | not null default nextval('gis.coordinate_id_coordinate_seq'::regclass) | plain |
location | geometry | | main |
Indexes:
"coordinate_pkey" PRIMARY KEY, btree (id_coordinate)
"ix_pk_coordinate" UNIQUE, btree (id_coordinate) CLUSTER
"ix_coordinate_location" gist (location)
Check constraints:
"enforce_dims_location" CHECK (ndims(location) = 2)
"enforce_geotype_location" CHECK (geometrytype(location) = 'POINT'::text OR location IS NULL)
"enforce_srid_location" CHECK (srid(location) = 2710)
最佳答案
这不是相同的数据,因此期望相同的计划是不合理的,除非统计数据(mnc = 41 的行数等等,值在整个表中的分布方式等)相似。
在一种情况下,该值很可能出现并遍布整个地方,而在另一种情况下,它们的分组范围很窄。在第一种情况下,seq 扫描行通常会更快;另一方面,索引扫描通常会更快。
关于sql - 相同的查询,不同的表,postgres 上的不同执行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27426435/
是 if(a == 0 && b == 0 && c == 0) { return; } 一样 if(a == 0) { return; } if(b == 0) { return; } if(c =
我想做这样的事情: Class A Class B extends A Class C extends A B b = new B(); C c = new C(); b->setField("foo
我对 Mysql 世界很天真......:)我试图使用连接从表中查询, 我遇到结果集问题...表结构如下 下面... VIDEO_XXXXX | Field | Type
我最近问过关于从另一个类获取类的唯一实例的问题。 ( How to get specific instance of class from another class in Java? ) 所以,我正
假设我们有两种类型 using t1 = int*; using t2 = int*; 我知道 std::is_same::value会给我们true .什么是,或者是否有模板工具可以实现以下目标?
对于我的一个应用程序,我假设比较 2 个字符串的第一个字符比比较整个字符串是否相等要快。例如,如果我知道只有 2 个可能的字符串(在一组 n 字符串中)可以以相同的字母开头(比如说 'q'),如果是这
我想在我的NXP LPC11U37H主板(ARM Cortex-M0)上分析一些算法,因为我想知道执行特定算法需要多少个时钟周期。 我编写了这些简单的宏来进行一些分析: #define START_C
我在 Excel 中创建了一个宏,它将在 Excel 中复制一个表格,并将行除以我确定的特定数字(默认 = 500 行),并为宏创建的每个部门打开不同的工作表。 使用的代码是这样的: Sub Copy
我想根据第一个字典对第二个字典的值求和。如果我有字典 A 和 B。 A = {"Mark": ["a", "b", "c", "d"], "June": ["e", "a"], "John": ["a
当我这样做时 system()在 Perl 中调用,我通常根据 perldocs 检查返回码.嗯,我是这么想的。大部分时间 $rc!=0对我来说已经足够了。最近我在这里帮助了两个遇到问题的人syste
在我的进度条上,我试图让它检测 div 加载速度。 如果 div 加载速度很快,我想要实现的目标将很快达到 100%。但进度条的加载速度应该与 div 的加载速度一样快。 问题:如何让我的进度条加载
当我获得与本地时间相同的时间戳时,firebase 生成的服务器时间戳是否会自动转换为本地时间,或者我错过了什么? _firestore.collection("9213903123").docume
根据the original OWL definition of OWL DL ,我们不能为类和个体赋予相同的名称(这是 OWL DL 和 OWL Full 之间的明显区别)。 "Punning" i
我有两个输入复选框: 尝试使用 jQuery 来允许两个输入的行为相同。如果选中第一个复选框,则选中第二个复选框。如果未检查第 1 个,则不会检查第 2 个。反之亦然。 我有代码: $('inpu
可以从不同系统编译两个相同的java文件,但它们都有相同的内容操作系统(Windows 7),会生成不同的.class文件(大小)? 最佳答案 是的,您可以检查是否有不同版本的JDK(Java Dev
我正在清理另一个人的正则表达式,他们目前所有的都以结尾 .*$ 那么下面的不是完全一样吗? .* 最佳答案 .*将尽可能匹配,但默认情况下为 .不匹配换行符。如果您要匹配的文本有换行符并且您处于 MU
我使用 Pick ,但是如何编写可以选择多个字段的通用PickMulti呢? interface MyInterface { a: number, b: number, c: number
我有一个 SQL 数据库服务器和 2 个具有相同结构和数据的数据库。我在 2 个数据库中运行相同的 sql 查询,其中一个需要更长的时间,而另一个在不到 50% 的时间内完成。他们都有不同的执行计划。
我需要你的帮助,我有一个包含两列的表,一个 id 和 numpos,我希望 id 和 numops 具有相同的结果。 例子: $cnx = mysql_connect( "localhost", "r
如何将相同的列(在本例中按“级别”排序)放在一起?我正在做一个高分,我从我的数据库中按级别列出它们。如果他们处于同一级别,我希望他们具有相同的 ID。 但是我不想在别人身上显示ID。只有第一个。这是一
我是一名优秀的程序员,十分优秀!