You write:
Each customer can have multiple sites, but only one should be
displayed in this list.
Yet, your query retrieves all rows. That would be a point to optimize. But you also do not define which site
is to be picked.
Either way, it does not matter much here. Your EXPLAIN
shows only 5026 rows for the site
scan (5018 for the customer
scan). So hardly any customer actually has more than one site. Did you ANALYZE
your tables before running EXPLAIN
?
From the numbers I see in your EXPLAIN
, indexes will give you nothing for this query. Sequential table scans will be the fastest possible way. Half a second is rather slow for 5000 rows, though. Maybe your database needs some general performance tuning?
Maybe the query itself is faster, but "half a second" includes network transfer? EXPLAIN ANALYZE would tell us more.
If this query is your bottleneck, I would suggest you implement a materialized view.
After you provided more information I find that my diagnosis pretty much holds.
The query itself needs 27 ms. Not much of a problem there. "Half a second" was the kind of misunderstanding I had suspected. The slow part is the network transfer (plus ssh encoding / decoding, possibly rendering). You should only retrieve 100 rows, that would solve most of it, even if it means to execute the whole query every time.
If you go the route with a materialized view like I proposed you could add a serial number without gaps to the table plus index on it - by adding a column row_number() OVER (<your sort citeria here>) AS mv_id
.
Then you can query:
SELECT *
FROM materialized_view
WHERE mv_id >= 2700
AND mv_id < 2800;
This will perform very fast. LIMIT
/ OFFSET
cannot compete, that needs to compute the whole table before it can sort and pick 100 rows.
pgAdmin timing
When you execute a query from the query tool, the message pane shows something like:
Total query runtime: 62 ms.
And the status line shows the same time. I quote pgAdmin help about that:
The status line will show how long the last query took to complete. If
a dataset was returned, not only the elapsed time for server execution
is displayed, but also the time to retrieve the data from the server
to the Data Output page.
If you want to see the time on the server you need to use SQL EXPLAIN ANALYZE
or the built in Shift + F7
keyboard shortcut or Query -> Explain analyze
. Then, at the bottom of the explain output you get something like this:
Total runtime: 0.269 ms
GRANT
ing ALL
permissions for public to the database is mostly redundant (as public has connect, temporary by default, so you'd only be adding CREATE
which you probably don't want to do). You probably expected a GRANT ALL
on the database to result in a recursive GRANT ALL
to contained schemas and tables. GRANT
is not recursive, so this doesn't happen; a GRANT ALL
on a database just grants CONNECT
and TEMPORARY
rights to the database, with no effect on contained schemas and tables.
The default GRANT
s are, from the docs on GRANT:
PostgreSQL grants default privileges on some types of objects to
PUBLIC. No privileges are granted to PUBLIC by default on tables,
columns, schemas or tablespaces. For other types, the default
privileges granted to PUBLIC are as follows: CONNECT and CREATE TEMP
TABLE for databases; EXECUTE privilege for functions; and USAGE
privilege for languages. The object owner can, of course, REVOKE both
default and expressly granted privileges. (For maximum security, issue
the REVOKE in the same transaction that creates the object; then there
is no window in which another user can use the object.) Also, these
initial default privilege settings can be changed using the ALTER
DEFAULT PRIVILEGES command.
So you can see you don't need to GRANT
anything on the database unless you want the user to be able to create schemas, etc. You need to:
GRANT USAGE ON SCHEMA myschema TO theuser;
for any schema other than public
. CREATE
can be granted if you want the user to be able to make tables, views, etc.
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE sometable TO theuser;
for tables. I've omitted the TRUNATE
, REFERENCES
and TRIGGER
rights as you probably don't want to grant them.
GRANT USAGE ON SEQUENCE sometable_somecolumn_seq TO theuser;
for any sequences that are used in table defaults, either explicitly or via a SERIAL
or BIGSERIAL
column.
... etc. See the manual for GRANT
linked above for full definitions of what the privileges do, which are available on which objects, etc. Take note of the wildcard ALL TABLES
and ALL SEQUENCES
options.
If this seems like too much hassle to do for each table, view, schema, sequence, etc, you can in Pg 9.1 and above use ALTER DEFAULT PRIVILEGES
to change the default GRANT
s on new objects.
Best Answer
If you are using PostgreSQL 9.2 or newer, you can use
RENAME CONSTRAINT
: