特定数据的描述排序性能不好-Postgresql 10.3
我有一个关于一个奇怪的(?)案例的问题,我发现这个案例是关于排序的 Postgresql(具体为10.3) 我有一个包含以下列的表特定数据的描述排序性能不好-Postgresql 10.3,sql,database,postgresql,sorting,postgresql-10,Sql,Database,Postgresql,Sorting,Postgresql 10,我有一个关于一个奇怪的(?)案例的问题,我发现这个案例是关于排序的 Postgresql(具体为10.3) 我有一个包含以下列的表users: id-varchar(36)-id为UUID格式 firstname-varchar(255), lastname-varchar(255) 将创建以下索引: create unique index users_pkey on users (id); create index user_firstname on users (firstname)
users
:
-id
-id为UUID格式varchar(36)
-firstname
,varchar(255)
-lastname
varchar(255)
create unique index users_pkey on users (id);
create index user_firstname on users (firstname);
create index user_lastname on users (lastname);
现在,让我们考虑每个数据集的两个查询。< /P>
firstname
是一个随机的10字符字符串
(1a)
以及此查询的执行计划:
Limit (cost=7665.06..7665.18 rows=50 width=48) (actual time=105.012..105.016 rows=50 loops=1)
-> Sort (cost=7665.06..7915.07 rows=100003 width=48) (actual time=105.012..105.014 rows=50 loops=1)
Sort Key: firstname, id
Sort Method: top-N heapsort Memory: 31kB
-> Seq Scan on users (cost=0.00..4343.03 rows=100003 width=48) (actual time=0.009..21.510 rows=100003 loops=1)
Planning time: 0.066 ms
Execution time: 105.031 ms
Limit (cost=7665.06..7665.18 rows=50 width=48) (actual time=105.586..105.590 rows=50 loops=1)
-> Sort (cost=7665.06..7915.07 rows=100003 width=48) (actual time=105.586..105.589 rows=50 loops=1)
Sort Key: firstname DESC, id DESC
Sort Method: top-N heapsort Memory: 31kB
-> Seq Scan on users (cost=0.00..4343.03 rows=100003 width=48) (actual time=0.010..21.670 rows=100003 loops=1)
Planning time: 0.068 ms
Execution time: 105.606 ms
Limit (cost=7665.06..7665.18 rows=50 width=43) (actual time=99.572..99.577 rows=50 loops=1)
-> Sort (cost=7665.06..7915.07 rows=100003 width=43) (actual time=99.572..99.573 rows=50 loops=1)
Sort Key: firstname, id
Sort Method: top-N heapsort Memory: 29kB
-> Seq Scan on users (cost=0.00..4343.03 rows=100003 width=43) (actual time=0.009..23.660 rows=100003 loops=1)
Planning time: 0.064 ms
Execution time: 99.592 ms
Limit (cost=7665.06..7665.18 rows=50 width=43) (actual time=659.786..659.791 rows=50 loops=1)
-> Sort (cost=7665.06..7915.07 rows=100003 width=43) (actual time=659.785..659.786 rows=50 loops=1)
Sort Key: firstname DESC, id DESC
Sort Method: top-N heapsort Memory: 32kB
-> Seq Scan on users (cost=0.00..4343.03 rows=100003 width=43) (actual time=0.010..21.510 rows=100003 loops=1)
Planning time: 0.066 ms
Execution time: 659.804 ms
1b)
排序已更改-desc而不是asc
以及此查询的执行计划:
Limit (cost=7665.06..7665.18 rows=50 width=48) (actual time=105.012..105.016 rows=50 loops=1)
-> Sort (cost=7665.06..7915.07 rows=100003 width=48) (actual time=105.012..105.014 rows=50 loops=1)
Sort Key: firstname, id
Sort Method: top-N heapsort Memory: 31kB
-> Seq Scan on users (cost=0.00..4343.03 rows=100003 width=48) (actual time=0.009..21.510 rows=100003 loops=1)
Planning time: 0.066 ms
Execution time: 105.031 ms
Limit (cost=7665.06..7665.18 rows=50 width=48) (actual time=105.586..105.590 rows=50 loops=1)
-> Sort (cost=7665.06..7915.07 rows=100003 width=48) (actual time=105.586..105.589 rows=50 loops=1)
Sort Key: firstname DESC, id DESC
Sort Method: top-N heapsort Memory: 31kB
-> Seq Scan on users (cost=0.00..4343.03 rows=100003 width=48) (actual time=0.010..21.670 rows=100003 loops=1)
Planning time: 0.068 ms
Execution time: 105.606 ms
Limit (cost=7665.06..7665.18 rows=50 width=43) (actual time=99.572..99.577 rows=50 loops=1)
-> Sort (cost=7665.06..7915.07 rows=100003 width=43) (actual time=99.572..99.573 rows=50 loops=1)
Sort Key: firstname, id
Sort Method: top-N heapsort Memory: 29kB
-> Seq Scan on users (cost=0.00..4343.03 rows=100003 width=43) (actual time=0.009..23.660 rows=100003 loops=1)
Planning time: 0.064 ms
Execution time: 99.592 ms
Limit (cost=7665.06..7665.18 rows=50 width=43) (actual time=659.786..659.791 rows=50 loops=1)
-> Sort (cost=7665.06..7915.07 rows=100003 width=43) (actual time=659.785..659.786 rows=50 loops=1)
Sort Key: firstname DESC, id DESC
Sort Method: top-N heapsort Memory: 32kB
-> Seq Scan on users (cost=0.00..4343.03 rows=100003 width=43) (actual time=0.010..21.510 rows=100003 loops=1)
Planning time: 0.066 ms
Execution time: 659.804 ms
firstname
是以下格式的字符串:johnxxx,其中XXXXX是数字序列,即John00000、John00001、John00002、John00003、…、John99998、John99999
2a)
以及此查询的执行计划:
Limit (cost=7665.06..7665.18 rows=50 width=48) (actual time=105.012..105.016 rows=50 loops=1)
-> Sort (cost=7665.06..7915.07 rows=100003 width=48) (actual time=105.012..105.014 rows=50 loops=1)
Sort Key: firstname, id
Sort Method: top-N heapsort Memory: 31kB
-> Seq Scan on users (cost=0.00..4343.03 rows=100003 width=48) (actual time=0.009..21.510 rows=100003 loops=1)
Planning time: 0.066 ms
Execution time: 105.031 ms
Limit (cost=7665.06..7665.18 rows=50 width=48) (actual time=105.586..105.590 rows=50 loops=1)
-> Sort (cost=7665.06..7915.07 rows=100003 width=48) (actual time=105.586..105.589 rows=50 loops=1)
Sort Key: firstname DESC, id DESC
Sort Method: top-N heapsort Memory: 31kB
-> Seq Scan on users (cost=0.00..4343.03 rows=100003 width=48) (actual time=0.010..21.670 rows=100003 loops=1)
Planning time: 0.068 ms
Execution time: 105.606 ms
Limit (cost=7665.06..7665.18 rows=50 width=43) (actual time=99.572..99.577 rows=50 loops=1)
-> Sort (cost=7665.06..7915.07 rows=100003 width=43) (actual time=99.572..99.573 rows=50 loops=1)
Sort Key: firstname, id
Sort Method: top-N heapsort Memory: 29kB
-> Seq Scan on users (cost=0.00..4343.03 rows=100003 width=43) (actual time=0.009..23.660 rows=100003 loops=1)
Planning time: 0.064 ms
Execution time: 99.592 ms
Limit (cost=7665.06..7665.18 rows=50 width=43) (actual time=659.786..659.791 rows=50 loops=1)
-> Sort (cost=7665.06..7915.07 rows=100003 width=43) (actual time=659.785..659.786 rows=50 loops=1)
Sort Key: firstname DESC, id DESC
Sort Method: top-N heapsort Memory: 32kB
-> Seq Scan on users (cost=0.00..4343.03 rows=100003 width=43) (actual time=0.010..21.510 rows=100003 loops=1)
Planning time: 0.066 ms
Execution time: 659.804 ms
2b)
排序已更改-desc而不是asc
以及此查询的执行计划:
Limit (cost=7665.06..7665.18 rows=50 width=48) (actual time=105.012..105.016 rows=50 loops=1)
-> Sort (cost=7665.06..7915.07 rows=100003 width=48) (actual time=105.012..105.014 rows=50 loops=1)
Sort Key: firstname, id
Sort Method: top-N heapsort Memory: 31kB
-> Seq Scan on users (cost=0.00..4343.03 rows=100003 width=48) (actual time=0.009..21.510 rows=100003 loops=1)
Planning time: 0.066 ms
Execution time: 105.031 ms
Limit (cost=7665.06..7665.18 rows=50 width=48) (actual time=105.586..105.590 rows=50 loops=1)
-> Sort (cost=7665.06..7915.07 rows=100003 width=48) (actual time=105.586..105.589 rows=50 loops=1)
Sort Key: firstname DESC, id DESC
Sort Method: top-N heapsort Memory: 31kB
-> Seq Scan on users (cost=0.00..4343.03 rows=100003 width=48) (actual time=0.010..21.670 rows=100003 loops=1)
Planning time: 0.068 ms
Execution time: 105.606 ms
Limit (cost=7665.06..7665.18 rows=50 width=43) (actual time=99.572..99.577 rows=50 loops=1)
-> Sort (cost=7665.06..7915.07 rows=100003 width=43) (actual time=99.572..99.573 rows=50 loops=1)
Sort Key: firstname, id
Sort Method: top-N heapsort Memory: 29kB
-> Seq Scan on users (cost=0.00..4343.03 rows=100003 width=43) (actual time=0.009..23.660 rows=100003 loops=1)
Planning time: 0.064 ms
Execution time: 99.592 ms
Limit (cost=7665.06..7665.18 rows=50 width=43) (actual time=659.786..659.791 rows=50 loops=1)
-> Sort (cost=7665.06..7915.07 rows=100003 width=43) (actual time=659.785..659.786 rows=50 loops=1)
Sort Key: firstname DESC, id DESC
Sort Method: top-N heapsort Memory: 32kB
-> Seq Scan on users (cost=0.00..4343.03 rows=100003 width=43) (actual time=0.010..21.510 rows=100003 loops=1)
Planning time: 0.066 ms
Execution time: 659.804 ms
对于第二个数据集,第二次查询(2b
)的速度要慢7倍
概括地说:
+----------------+------------+------------+
| Query\Data set | 1 | 2 |
+----------------+------------+------------+
| 1 | 105.031 ms | 99.592 ms |
| 2 | 105.606 ms | 659.804 ms |
+----------------+------------+------------+
最后,我的问题。
为什么对第二个数据集的第二次查询比其他情况慢6-7倍?在添加额外的50k数据后,您是否重建了索引?检查碎片。在添加额外的50k数据后,是否重新构建了索引?检查碎片。重新生成索引。这些问题发生在具有新数据的新表上,因此我的假设是碎片问题不是这种情况,但可以肯定的是,我如何检查碎片?转到表->索引,右键单击索引,选择属性,然后在选项卡的左侧菜单上选择碎片。您使用的是什么postgresql客户端?我在pgAdmin和Intellij中都找不到这个选项。我最真诚的道歉,我以为你在使用SSMS。关于这一点,我的同事发现这篇文章特别有用:()重建索引。这些问题发生在具有新数据的新表上,因此我的假设是碎片问题不是这种情况,但可以肯定的是,我如何检查碎片?转到表->索引,右键单击索引,选择属性,然后在选项卡的左侧菜单上选择碎片。您使用的是什么postgresql客户端?我在pgAdmin和Intellij中都找不到这个选项。我最真诚的道歉,我以为你在使用SSMS。关于这一点,我的同事们发现这篇文章特别有用:()