2

I have a table in my DB that holds configuration settings for some apps along with versions of these configs:

app_id | config  | version
--------------------------
app1   | conf1v1 |   1
app2   | conf2v1 |   1
app1   | conf1v2 |   2

I need to get the latest configurations (the ones with greatest version numbers) for each application. I use a query like this:

select c.app_id, c.config, c.version
from config c       
where 
  c.version=(select max(c2.version) from config c2 where c2.app_id = c.app_id)

But it seems to be inefficient. I wonder, are there more efficient ways to do this task?

8
  • What do you mean when you say that it "seems to be inefficient"? Did you actually measure? Did you investigate the execution plan? On the face of it, I don't see an obvious reason for this query to be inefficient, provided all needed indexes are in place. Nov 15, 2011 at 17:20
  • @BrankoDimitrijevic I believe that select in where clause will cause a table hit for each row, returned by query. Nov 15, 2011 at 17:24
  • @BrankoDimitrijevic What indices should I create for this table? Nov 15, 2011 at 17:28
  • @m0skit0 - in what what does this denote that sql sucks? The ROW_NUMBER() solution translates to "for each app_id, pick only the first item ordered by version in descending order". This potentially enables an index seek for each app_id (with appropriate indexes). Perhaps your comment denotes just how much you misunderstand analytic functions in SQL?
    – MatBailie
    Nov 15, 2011 at 17:37
  • I'm no SQL expert of course, but this just denotes how much stuff you need to write to do quite simple things... That means an underlying bad design and I'm pretty sure I'm not the only one that sees bad design in SQL. Being an expert on SQL shouldn't blind you to see the flaws on SQL. Anyway I didn't meant to divert the question, sorry.
    – m0skit0
    Nov 15, 2011 at 17:40

3 Answers 3

4

You can use an analytic function. That will be at least a bit more efficient since it avoids hitting the table twice.

SELECT app_id, config, version
  FROM (SELECT app_id,
               config,
               version,
               row_number() over (partition by app_id order by version desc) rnk
          FROM config)
 WHERE rnk = 1
1

Combining a CTE with ROW_NUMBER() will allow a single parse of the table (using an index if available, to further reduce reads)...

WITH
  reversioned_config
AS
(
  SELECT
    *,
    ROW_NUMBER() OVER (PARTITION BY app_id ORDER BY version DESC) as descending_version
  FROM
    config
)
SELECT
  app_id,
  config,
  version
FROM
  reversioned_config
WHERE
  descending_version = 1
0
0

What about

select app_id, config, MAX(version) version  from config 
group by app_id, config
1
  • Since we assume that configs are different, query will return duplicate rows for different configs of apps. Nov 15, 2011 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.