Opened 15 years ago

Last modified 14 years ago

#1555 new enhancement

A more efficient SQL query for uniqueness

Reported by: gjm Owned by: nobody
Priority: minor: annoyance Milestone: Version 1.7.0
Component: Data Provider Version: Trunk
Keywords: Cc:
Must Fix for Release: No Platform: All
Platform Version: Awaiting user input: no

Description

The postgres provider, at times, runs a query on a table to see if a particular column contains unique values. This is done as part of choosing a suitable column to use as an index for that table. The SQL that does this uniqueness check is in the uniqueData() function in the qgspostgresprovider.cpp file. The SQL is:

select count(distinct %1)=count(%1) from %2.%3

where %1 is the column in question, %2 the schema name and %3 the table name.

This counts the number of rows in that row almost twice. A potentially more efficient way to achieve the same outcome is with an SQL like this:

select count(*) from (select %1 from %2.%3 group by %1 having count(*) > 1 limit 1) as foo;

This would return 0 or 1, depending if there were unique (or not) data in row %1.

This needs a little bit of testing first to check that it does reduce the query time (I don't have the time at the moment).

Change History (4)

comment:1 by jcs, 15 years ago

It's still not clear to me that trying to guess the index column is the best approach. I contend that the user will always know more about the database than the application, so let the user say what column to use. I have a patch for this feature, see ticket #1535 if interested.

comment:2 by jef, 15 years ago

another candidate:

SELECT NOT EXISTS (SELECT %1 FROM %2.%3 GROUP BY %1 HAVING COUNT(*)>1) as isunique;

comment:3 by pcav, 15 years ago

Milestone: Version 1.0.3

comment:4 by pcav, 14 years ago

Milestone: Version 1.0.3Version 1.6.0
Note: See TracTickets for help on using tickets.