Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Index spanning multiple tables in PostgreSQL

Tags:

sql

postgresql

Is it possible in PostgreSQL to place an index on an expression containing fields of multiple tables. So for example an index to speed up an query of the following form:

SELECT *, (table1.x + table2.x) AS z
FROM table1
INNER JOIN table2
ON table1.id = table2.id
ORDER BY z ASC
like image 482
Ewout Kleinsmann Avatar asked May 06 '13 15:05

Ewout Kleinsmann


People also ask

Can Postgres use multiple indexes at once?

Fortunately, PostgreSQL has the ability to combine multiple indexes (including multiple uses of the same index) to handle cases that cannot be implemented by single index scans. The system can form AND and OR conditions across several index scans.

Can we CREATE INDEX multiple tables?

The answer is no. I can't create a index on multiple tables. My solution would be to make a table instead, and in my case a view also, or I could just optimize the SQL code.

Can we CREATE INDEX on multiple columns in PostgreSQL?

You can create an index on more than one column of a table. This index is called a multicolumn index, a composite index, a combined index, or a concatenated index. A multicolumn index can have maximum 32 columns of a table. The limit can be changed by modifying the pg_config_manual.

Can we join 3 tables in PostgreSQL?

2) Using PostgreSQL INNER JOIN to join three tables And each payment is processed by one and only one staff.


2 Answers

No it's not possible to have an index on many tables, also it really wouldn't guarantee speeding up anything since you won't always get an Index Only Scan. What you really want is a materialized view but pg doesn't have those either. You can try implementing it yourself using triggers like this or this.


Update

As noted by @petter. The materialized views were introduced in 9.3.

like image 186
Jakub Kania Avatar answered Oct 19 '22 02:10

Jakub Kania


No, that's not possible in any currently shipping SQL dbms. Oracle supports bitmap join indexes, but that might not be relevant. It's not clear to me whether you want an index on only the join columns of multiple tables, or whether you want an index on arbitrary columns of joined tables.

To determine the real source of performance problems, learn to read the output of PostgreSQL's EXPLAIN ANALYZE.

like image 35
Mike Sherrill 'Cat Recall' Avatar answered Oct 19 '22 04:10

Mike Sherrill 'Cat Recall'