Question

Disclaimer: this is not for optimization, just out of curiosity.

I'm wondering if this:

SET search_path TO myscheme; -- obviously this is done once per connection
SELECT foo, bar FROM table1 WHERE [..clauses..]

is somehow faster / slower than

SELECT foo, bar FROM myscheme.table1 WHERE [..clauses..]

or if there are some other implications that could suggest specifying the schema (or not) in every query.

I've done some (really few) tests and I can't see any difference in terms of speed.

Was it helpful?

Solution

The second one is faster, but barely. SET is extremely cheap.
Generally, a schema-qualified table name has the potential to be slightly faster, since the query to the system catalog can be more specific. But you won't be able to measure that. It's just irrelevant, performance-wise.

Setting the search_path does have implications for security and convenience though. It is generally a good idea.
For instance, consider the advice in the manual on "Writing SECURITY DEFINER Functions Safely".

Are you aware that there are many ways to set the search_path?

OTHER TIPS

For all practical purposes this should make any measurable difference.

IF there is a measurable difference, I would guess, that the system has

  • a search_path with very many schema entries (several hundreds) and/or
  • an extreme number of relations (so many that the relation catalogue does not fit into memory for the normal workload).

In that case you have definitely other problems than qualifying you schema names.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top