J'avais une requête qui prenait beaucoup de temps à s'exécuter. Je l'ai donc réécrite et maintenant elle ne prend presque pas de temps à s'exécuter - mais je ne comprends pas pourquoi.
Je peux comprendre une petite différence, mais quelqu'un peut-il m'aider à expliquer l'énorme différence de temps nécessaire pour exécuter ces deux déclarations (qui semblent très similaires) ?
D'abord :
DELETE FROM t_old where company_id not in (select company_id from t_prop);
Deuxièmement :
DELETE FROM t_old a
using t_prop b
where a.company_id=b.company_id
and b.company_id is null;
Plan d'exécution du premier :
'[
{
"Plan": {
"Startup Cost": 0,
"Plans": [
{
"Filter": "(NOT (SubPlan 1))",
"Startup Cost": 0,
"Plans": [
{
"Startup Cost": 0,
"Plans": [
{
"Startup Cost": 0,
"Node Type": "Seq Scan",
"Plan Rows": 158704,
"Relation Name": "t_prop",
"Alias": "t_prop",
"Parallel Aware": false,
"Parent Relationship": "Outer",
"Plan Width": 4,
"Total Cost": 2598.04
}
],
"Node Type": "Materialize",
"Plan Rows": 158704,
"Parallel Aware": false,
"Parent Relationship": "SubPlan",
"Plan Width": 4,
"Subplan Name": "SubPlan 1",
"Total Cost": 4011.56
}
],
"Node Type": "Seq Scan",
"Plan Rows": 21760,
"Relation Name": "t_old",
"Alias": "t_old",
"Parallel Aware": false,
"Parent Relationship": "Member",
"Plan Width": 6,
"Total Cost": 95923746.03
}
],
"Node Type": "ModifyTable",
"Plan Rows": 21760,
"Relation Name": "t_old",
"Alias": "t_old",
"Parallel Aware": false,
"Operation": "Delete",
"Plan Width": 6,
"Total Cost": 95923746.03
}
} ]'
Plan d'exécution du second
'[
{
"Plan": {
"Startup Cost": 0.71,
"Plans": [
{
"Startup Cost": 0.71,
"Plans": [
{
"Startup Cost": 0.42,
"Scan Direction": "Forward",
"Plan Width": 10,
"Node Type": "Index Scan",
"Index Cond": "(company_id IS NULL)",
"Plan Rows": 1,
"Relation Name": "t_prop",
"Alias": "b",
"Parallel Aware": false,
"Parent Relationship": "Outer",
"Total Cost": 8.44,
"Index Name": "t_prop_idx2"
},
{
"Startup Cost": 0.29,
"Scan Direction": "Forward",
"Plan Width": 10,
"Node Type": "Index Scan",
"Index Cond": "(company_id = b.company_id)",
"Plan Rows": 5,
"Relation Name": "t_old",
"Alias": "a",
"Parallel Aware": false,
"Parent Relationship": "Inner",
"Total Cost": 8.38,
"Index Name": "t_old_idx"
}
],
"Node Type": "Nested Loop",
"Plan Rows": 5,
"Join Type": "Inner",
"Parallel Aware": false,
"Parent Relationship": "Member",
"Plan Width": 12,
"Total Cost": 16.86
}
],
"Node Type": "ModifyTable",
"Plan Rows": 5,
"Relation Name": "t_old",
"Alias": "a",
"Parallel Aware": false,
"Operation": "Delete",
"Plan Width": 12,
"Total Cost": 16.86
}
} ]'