sapronovps / pgsqlindexanalyzer
PgsqlIndexAnalyzer - 这是一个简单的PHP库,可以帮助支持PostgreSQL数据库中的索引。
1.0.0
2023-01-13 12:52 UTC
Requires
- php: >=8.0
- ext-pdo: *
Requires (Dev)
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.5
README
简介
PgsqlIndexAnalyzer - 这是一个简单的PHP库,可以帮助支持PostgreSQL数据库中的索引。
注意! 此库仅作为推荐。所有获得的索引都需要进行分析。
- 表的所有索引
- 表的无用索引
- 表的重复索引
- 其他表索引中包含的索引
目录
安装
composer require sapronovps/pgsqlindexanalyzer --dev
方法
库中仅包含4个方法
- allIndexesByTables - 方法返回表的所有索引。
- unusedIndexesByTables - 方法返回表的无用索引。无用索引由参数 IndexScan = 0 确定
- overlappingIndexesByTables - 方法返回表的重复索引。
- indexesContainsInOtherIndexesByTables - 方法返回其他表索引中包含的索引。
用法
首先您需要创建库的实例并估算配置。
<?php use Sapronovps\PgsqlIndexAnalyzer\Connection\Connection; use Sapronovps\PgsqlIndexAnalyzer\Option\Options; use Sapronovps\PgsqlIndexAnalyzer\PgsqlIndexAnalyzer; // Create options as array OR with Options class. $options = [ 'host' => 'localhost', 'dbName' => 'postgresql', 'user' => 'postgresql', 'password' => 'secretPassword', ]; // OR $options = new Options(); $options->setHost('localhost') ->setDbName('postgresql') ->setUser('postgresql') ->setPassword('secretPassword'); $connection = new Connection($options); $pgsqlIndexAnalyzer = new PgsqlIndexAnalyzer($connection);
获取表的所有索引
$tables = [ 'table1', 'table2', 'table3', ]; $allIndexes = $pgsqlIndexAnalyzer->allIndexesByTables($tables);
获取表的无用索引
无用索引 - 当参数 "IndexScan" === 0 时为无用索引;
$tables = [ 'table1', 'table2', 'table3', ]; $unusedIndexesByTables = $pgsqlIndexAnalyzer->unusedIndexesByTables($tables);
获取表的重复索引
重复索引 - 在PostgreSQL中,索引是从左到右读取的,因此存在一些重复且在严格左到右顺序中已包含在任何现有索引中的索引。通常,这样的索引可以被删除。
$tables = [ 'table1', 'table2', 'table3', ]; $overlappingIndexesByTables = $pgsqlIndexAnalyzer->overlappingIndexesByTables($tables);
获取其他表索引中包含的索引
其他表索引中包含的索引 - 此方法与 overlappingIndexesByTables 非常相似,但此方法寻找重复索引时不需要考虑从左到右的读取顺序。注意:此类索引只能在详细分析后删除。
$tables = [ 'table1', 'table2', 'table3', ]; $indexesContainsInOtherIndexesByTables = $pgsqlIndexAnalyzer->indexesContainsInOtherIndexesByTables($tables);
测试
此库由单元测试和phpstan覆盖。