

Yes, I would like to receive information about new products, current offers and news about PostgreSQL via e-mail on a regular basis.įurther information can be found in the privacy policy. Wyrażenie zgody na otrzymywanie Newslettera Cybertec drogąĮlektroniczną jest dobrowolne i może zostać w każdej chwili bezpłatnie odwołane.Więcej informacji Tak, chcę regularnie otrzymywać wiadomości e-mail o nowych produktach, aktualnych ofertach i
#POSTGRESQL WHERE FREE#
Granting consent to receive the Cybertec Newsletter by electronic means is voluntary and can be withdrawn free of charge at any time.įurther information can be found in the privacy policy.

Yes, I would like to receive information about new products, current offers and news about PostgreSQL via e-mail on a regular basis. Granting consent to receive the CYBERTEC Newsletter by electronic means is voluntary and can be withdrawn free of charge at any time.įurther information can be found in the privacy policy.

Weitere Informationen finden Sie in der Datenschutzerklärung. Ich kann diese Zustimmung jederzeit widerrufen. Ja, ich möchte regelmäßig Informationen über neue Produkte, aktuelle Angebote und Neuigkeiten rund ums Thema PostgreSQL per E-Mail erhalten. I find that this is hardly ever a problem in practice. In our case, we don’t have to worry, because the primary keys were included in the query result. When using this trick, you should be aware that rewriting a query in that fashion does not always result in an equivalent query: if the original query can return identical rows, these would be removed by the UNION. If you can be certain that both branches of the query will return distinct sets, it is better to use UNION ALL instead of UNION, because that doesn’t have to do the extra processing to remove duplicates. Avoiding the ugly ORįortunately, there is an equivalent query that is longer to write, but much cheaper to execute:īoth parts of the query can make use of efficient index scans and return one row, and since the rows happen to be identical, UNION will reduce them to one row. In our example, that would mean computing 100000 rows only to throw away the 99999 that do not match the condition. Here we have to compute the complete join between the two tables and afterwards filter out all rows matching the condition. Things become really bad if OR combines conditions from different tables: To avoid that, you can also use a GiST index, which is much smaller, but less efficient to search. Note 2: The GIN index can become quite large. Note 1: This index can also be used if the search pattern starts with %

But there is a simple method to rewrite that query without the pesky OR: Now for a more stupid variant of the above query:Īgain, a bitmap index scan is used. Moreover, it uses much more RAM each of these bitmaps can use up to work_mem memory.Ī multi-column index on (id, a_val) won’t help at all with this query, so there is no cheaper way to execute it. Note, however, that a bitmap index scan is more expensive than a normal index scan, since it has to build the bitmap. PostgreSQL can actually use an index scan for the query, because it can combine the bitmaps for both indexes with a “bitmap OR”. Now for an example of an OR in a WHERE clause that is still pretty nice: Unfortunately you usually find the OR where it hurts: in the WHERE clause. So if your OR appears in a CASE expression in the SELECT list, don’t worry. The “good” ORĪn OR is fine in most parts of an SQL query: if it is not used to filter out rows from your query result, it will have no negative effect on query performance. Have a look at the documentation if you don’t understand text_pattern_ops. Suppose that we want to run queries with equality and LIKE conditions on the text columns, so we need some indexes:ĬREATE INDEX a_val_idx ON a(a_val text_pattern_ops) ĬREATE INDEX b_val_idx ON b(b_val text_pattern_ops) We’ll use this simple setup for demonstration:ĬREATE TABLE a(id integer NOT NULL, a_val text NOT NULL) ĬREATE TABLE b(id integer NOT NULL, b_val text NOT NULL) ĪLTER TABLE b ADD FOREIGN KEY (id) REFERENCES a In this article I’ll explore “good” and “bad” ORs and what you can do to avoid the latter. But you should be aware of the performance implications. Of course there is a reason why there is an OR in SQL, and if you cannot avoid it, you have to use it. PostgreSQL query tuning is our daily bread at CYBERTEC, and once you have done some of that, you’ll start bristling whenever you see an OR in a query, because they are usually the cause for bad query performance. Or performance query sql © Laurenz Albe 2018
