数据库
首页 > 数据库> > mysql-通过删除SQL数据库中的行提高查询性能?

mysql-通过删除SQL数据库中的行提高查询性能?

作者:互联网

我有一个带有单个表的数据库,用于跟踪用户状态.处理完该行后,不再需要将其保留在数据库中,可以将其删除.

现在假设我想跟踪该行而不是删除该行(出于历史目的,分析等).这样做会更好吗?

>将数据保留在同一表中,并将该行标记为“已使用”(带有额外的列或类似内容)
>从表中删除行,并将其插入到单独的表中,该表仅用于历史目的

对于选择#1,我想知道是否将不必要的行留在数据库中会开始影响查询性能. (我所有的查询都在索引列上,所以也许没关系吗?)

对于选择#2,我想知道是否不断删除行会导致诸如碎片之类的问题?

解决方法:

从长远来看,查询性能会更好:

永久插入会发生什么:

The table grows, indexes grow, index performance (lookup) is decreases with the size of the table, especially insert performance is hurt.

删除发生了什么:

Table pages get fragmented, so the deleted space is not re-used 100% as expected, more near 50% in MySQL. So the table still grows to about twice the size you might expect for your amount of data. The index gets fragmented and becomes lob sided: It contains your new data but also the structure for your old data. It depends on the structure of your data on how bad this gets. This situation however stabilizes at a certain performance. This performance point has 2 benefits:

1) The table is more limited in size, so potential full table scans are faster

2) Your performance is predictable.

Due to the fragmentation however this performance point is not equal to about twice your data amount, it tends to be a bit worse (benchmark it to see yourself). The benefit of the delete scenario is however since you have a smaller data set, that you might be able to rebuild your index once every reasonable period, thus improving your performance.

备择方案

您可以查看两种替代方法来提高性能:

>切换到MariaDB:这在大型数据集上可获得约8%的性能(我的观察,数据集压缩后的数据仅为200GB)
>查看分区:如果您有方便的分区参数,则可以为您创建一系列“小表”,并防止进行删除,重建和历史数据管理的逻辑.这可能会为您提供最佳性能.

标签:performance,database-performance,mysql,database
来源: https://codeday.me/bug/20191027/1946094.html