In PostgreSQL, indexes on expressions (also known as “functional indexes”) allow you to create an index not just on a column but on the result of an expression or function applied to a column (or multiple columns). This can significantly improve query performance when the same expression is used frequently in queries, especially for complex calculations or transformations.
Continue readingConverting Between Uppercase and Lowercase in PostgreSQL
PostgreSQL provides us with a few functions that we can use to convert strings between uppercase and lowercase.
We can convert the whole string to uppercase or lowercase, or we can convert it to initial caps, where the first letter of each word is uppercase and the rest of the word is lowercase.
Continue readingFix “Cannot create index on view because the view is not schema bound” in SQL Server (Error 1939)
If you’re getting an error that reads something like “Cannot create index on view ‘MyView’ because the view is not schema bound” in SQL Server, it sounds like you’re trying to create an index on a view that’s not schema bound.
Indexes can only be created on schema bound views.
To fix this issue, define the view as schema bound before creating the index.
Continue readingPrepared Statements in PostgreSQL: A Complete Guide
Prepared statements are a feature in PostgreSQL that can improve performance and security when executing repetitive SQL queries.
By creating a prepared statement, the server can parse and plan the query once and reuse it multiple times, leading to faster execution. Prepared statements also help in preventing SQL injection attacks by safely handling user input.
In this article, we’ll explore prepared statements in PostgreSQL, discuss how to create and use them, and cover other important considerations such as query planning and cleaning up statements.
Continue reading3 Quick Examples of SQL Subqueries in the WHERE Clause
SQL subqueries are like little helpers that fetch data for our main query. They’re super handy when we need to filter our results based on some other data in our database.
Probably the most common placement of a subquery in SQL is in the WHERE
clause. Placing a subquery in the WHERE
clause allows us to compare a column against a set of rows that match a certain criteria.
Let’s look at three quick examples of subqueries in the WHERE
clause.
Fix “Computed column … cannot be persisted because the column is non-deterministic” in SQL Server (Error 4936)
If you’re getting an error that reads something like “Computed column ‘c3’ in table ‘t1’ cannot be persisted because the column is non-deterministic” in SQL Server, it appears that you’re trying to persist a computed column that’s nondeterministic.
A nondeterministic function or expression returns a different result for the same input. If a column uses such an expression, then it will be nondeterministic. A computed column must be deterministic.
Continue readingUnderstanding Partial Indexes in PostgreSQL
Adding indexes to a table is a well established practice for speeding up regular queries in relational databases such as PostgreSQL. While they can do wonders for query performance, indexes can also take up a lot of disk space.
Today we’re going to be looking at a particular type of index that can help reduce the impact on disk space, and query performance – the partial index.
Continue reading4 Ways to Create a UNIQUE Constraint in SQL Server
A UNIQUE
constraint is a rule that we can apply to one or more columns in a database table to ensure that the values in those columns are unique across all rows.
In SQL Server we have a few options when it comes to creating a UNIQUE
constraint. But it’s usually done when we create the table or alter it. That is, we include the constraint code in the CREATE TABLE
statement or the ALTER TABLE
statement.
We can also create a UNIQUE
index, which can be used in place of the previous methods, but can have the same effect.
Let’s check out several ways to create a UNIQUE
constraint in SQL Server.
Fix “Cannot create index on view because it uses the aggregate COUNT. Use COUNT_BIG instead.” in SQL Server (Error 10138)
If you’re getting an error that reads something like “Cannot create index on view “demo.dbo.SalesSummary” because it uses the aggregate COUNT. Use COUNT_BIG instead” in SQL Server, it’s because you’re trying to create an index on a view that uses the COUNT()
function.
Clustered vs Nonclustered Index: What’s the Difference?
Indexes can play a crucial role in optimizing SQL query performance. A well designed index can help a query perform much faster than it otherwise might. While there are many types of indexes, two common index types are clustered and nonclustered indexes.
Understanding the difference between clustered and nonclustered indexes can have a significant impact on our database design and query optimization strategies.
In this article, we’ll take a look at clustered and nonclustered indexes as implemented in SQL Server, explore their characteristics, and examine when to use each type.
Continue reading