"indexmattersontype" btree (type) "indexmattersontypeandid" btree (type, id) do you…
- User
"index_matters_on_type" btree (type) "index_matters_on_type_and_id" btree (type, id)
do you think this indexes in postgres too many?
- Assistant
In PostgreSQL (or any relational database), the number of indexes can greatly affect performance, both positively and negatively. Let’s analyze the two indexes you've mentioned:
-
index_matters_on_type(btree on ): This index allows for efficient queries that filter or sort by the column. If you frequently query this column alone, this index is useful.
-
- User
\d matters_tags Table "public.matters_tags" Column | Type | Collation | Nullable | Default
------------+--------------------------------+-----------+----------+------------------------------------------ id | bigint | | not null | nextval('matters_tags_id_seq'::regclass) matter_id | bigint | | not null | tag_id | bigint | | not null | created_at | timestamp(6) without time zone | | not null | updated_at | timestamp(6) without time zone | | not null | Indexes: "matters_tags_pkey" PRIMARY KEY, btree (id) "index_matters_tags_on_matter_id" btree (matter_id) "index_matters_tags_on_tag_id" btree (tag_id) Foreign-key constraints: "fk_rails_ec5b55dcc8" FOREIGN KEY (matter_id) REFERENCES matters(id) "fk_rails_ff9208e9e9" FOREIGN KEY (tag_id) REFERENCES tags(id)what do you think about this table?
- Assistant
- User
\d content_blocks Table "public.content_blocks" Column | Type | Collation | Nullable | Default
-----------------+--------------------------------+-----------+----------+-------------------------------------------- id | bigint | | not null | nextval('content_blocks_id_seq'::regclass) type | character varying | | not null | resource_id | integer | | not null | resource_type | character varying | | not null | content | jsonb | | | created_at | timestamp(6) without time zone | | not null | updated_at | timestamp(6) without time zone | | not null | position | integer | | | 0 tag_id | bigint | | | quote_signature | character varying | | | Indexes: "content_blocks_pkey" PRIMARY KEY, btree (id) "index_cb_resource_id_and_resource_type" btree (resource_id, resource_type) "index_content_blocks_on_position" btree ("position") "index_content_blocks_on_resource_id" btree (resource_id) "index_content_blocks_on_resource_type" btree (resource_type) "index_content_blocks_on_tag_id" btree (tag_id) "index_content_blocks_on_type" btree (type) Foreign-key constraints: "fk_rails_acbab2ee3c" FOREIGN KEY (tag_id) REFERENCES tags(id) - Assistant
- User
how to view index efficiency, like is it really useful
- Assistant
- User
i heard, that index is good if it's values is equally spreaded, like if it is true / false on huge table - it is bad index, but if it is like 1,2,3,4,5,6 and every number is accorded to let'say 1000000 records in table with 6000000 records, it is good index.
- Assistant
- User
is it possible somehow to view this numbers to estimate selectivity and cardinality for some index?
- Assistant
- User
SELECT position, COUNT(*) AS count FROM content_blocks GROUP BY position ORDER BY count DESC;
what do you think about this values?
position | count
----------+---------- | 16358762 1 | 2400918 0 | 601812 2 | 564009 3 | 272286 4 | 157310 5 | 94212 6 | 66863 7 | 47615 8 | 37898 9 | 30091 10 | 24420 11 | 19054 12 | 15473 13 | 12776 14 | 10753 15 | 9061 16 | 7793 17 | 6638 18 | 5877 19 | 5197 20 | 4513 21 | 3878 22 | 3451 23 | 3003 24 | 2702 25 | 2247 26 | 1995 27 | 1819 28 | 1620 29 | 1467 30 | 1315 31 | 1198 32 | 1089 33 | 945 34 | 885 35 | 792 36 | 749 37 | 663 38 | 615 39 | 543 40 | 476 41 | 435 42 | 392 43 | 340 44 | 311 45 | 287 46 | 262 - Assistant
- User
SELECT position, COUNT(*) AS count FROM content_blocks GROUP BY position ORDER BY count DESC;
could you adapt this query, so it print selectivity cardinality as numbers
- Assistant
Certainly! To adapt your SQL query to calculate and print both selectivity and cardinality for the
positioncolumn in the table, you'll first need to understand the definitions: - User
great, but i thought about something, like this index is good, or bad. like overall rate
- Assistant
- User
i mean not line by line, but total rating for index
- Assistant
- User
something wrong with your query - column overall_count not exists
- Assistant
I apologize for the oversight in my previous query. You are correct; the
overall_countcolumn does not exist because it was not defined in the final selection. Let's correct the query to properly calculate and return the necessary values. - User
how to view physical size of index
- Assistant