Create a Partial Index in PostgreSQL

In PostgreSQL, a partial index is an index that’s built on a subset of the rows in a table. It includes only the rows that satisfy a given condition specified with the WHERE clause.

Partial indexes can help us achieve faster queries, reduced storage, and lower overhead for write-heavy workloads.

Below is a quick example that demonstrates how to create a partial index in PostgreSQL.

Continue reading

Check How Many Times a Given Query Plan was Chosen for a Prepared Statement in PostgreSQL

When a prepared statement is created in PostgreSQL, the server has two possible strategies for executing it: use a generic plan or use a custom plan. Each time we run a prepared statement it will chose one of these two plans.

By default, it will use a custom plan for the first five runs. After that it will decide whether or not it’s more efficient to use a generic plan for subsequent executions, or stay with a custom plan.

This begs the question; how do we check to see which plan PostgreSQL chose?

Continue reading

Possible Reason You’re Getting an Error When using a Qualified Table Name with MySQL’s HANDLER Statement

If you’re getting MySQL error 1064 which reads something like “1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘READ FIRST’ at line 1” when using the HANDLER statement in MySQL, it could be that you’re qualifying the table with the database name when trying to read from or close a table.

Continue reading

SQL Server OUTPUT Clause: Using Expressions to Analyze Changes to the Data

When we use the OUTPUT clause in SQL Server, we have the option of providing expressions instead of just the column names. For example, we could use an expression that compares the old price with the new price and returns the difference. Such data could be handy, depending on what your goals are.

Let’s dive straight into an example that uses an expression in the OUTPUT clause.

Continue reading

Fix “Ambiguous column name” in SQL Server (Error 209)

If you’re getting an error that reads “Ambiguous column name“, followed by a column name, in SQL Server, it could be that you’re performing a join on two tables, but you’re not using the table names or aliases when referring to columns within those tables.

To fix this issue, be sure to qualify the columns with their table names or aliases.

Continue reading

Fix “An aggregate may not appear in the OUTPUT clause” in SQL Server (Error 158)

If you’re getting an error that reads “An aggregate may not appear in the OUTPUT clause” in SQL Server, it appears that you’re using an aggregate function in the OUTPUT clause.

Aggregate functions are not allowed in the OUTPUT clause.

To fix this issue, remove any aggregate functions from the OUTPUT clause and try applying them to a table or table variable.

Continue reading

Using the INCLUDE Option on an Index in SQL Server

The INCLUDE option in SQL Server allows us to include non-key columns in a nonclustered index. These columns are not part of the index key (which SQL Server uses to order and search the index), but they are stored with the index pages.

The INCLUDE option can significantly improve query performance when additional columns are needed by a query but are not part of the index key.

Continue reading

Example of PostgreSQL Automatically Creating a Nested Composite Type

Whenever we create a table in PostgreSQL, a composite type is automatically created behind the scenes. This composite type is based on the table that we created. Each column in the table becomes a field in the composite type.

If the table already uses a composite type, then the composite type that PostgreSQL creates will include that type in its definition, thereby creating a situation where we effectively have a nested composite type.

Continue reading