Select last record from data table for each device in devices table

Binakot

I have a problem with the executing speed of my sql query to postgres database.

I have 2 tables:

table 1: DEVICES

ID | NAME
------------------
1  | first device
2  | second device

table 2: DATA

ID | DEVICE_ID | TIME                | DATA
--------------------------------------------
1  | 1         | 2016-07-14 2:00:00  | data1
2  | 1         | 2016-07-14 1:00:00  | data2
3  | 2         | 2016-07-14 4:00:00  | data3
4  | 1         | 2016-07-14 3:00:00  | data4
5  | 2         | 2016-07-14 6:00:00  | data5
6  | 2         | 2016-07-14 5:00:00  | data6

I need get this select's result table:

ID | DEVICE_ID | TIME               | DATA
-------------------------------------------
4  | 1         | 2016-07-14 3:00:00 | data4
5  | 2         | 2016-07-14 6:00:00 | data5

i.e. for each device in devices table I need to get only one data record with the last TIME value.

This is my sql query:

SELECT * FROM db.data d 
    WHERE d.time = (
        SELECT MAX(d2.time) FROM db.data d2 
             WHERE d2.device_id = d.device_id);

This is HQL query equivalent:

SELECT d FROM Data d 
    WHERE d.time = (
        SELECT MAX(d2.time) FROM Data d2 
            WHERE d2.device.id = t2.device.id)

Yes, I use Hibernate ORM in my project - may this info will be useful for someone.

I got correct answer on my queries, BUT it's too long - about 5-10 seconds on 10k records in data table and only 2 devices in devices table. It's terrible.

First of all, I thought that problem is in Hibernate. But native sql query from psql in linux terminal execute the same time as through hibernate.

How can I optimize my query? This query is too complexity:

 O(device_count * data_count^2)
Chitharanjan Das

Since you're using Postgres, you could use window functions to achieve this, like so:

select
    sq.id,
    sq.device_id,
    sq.time,
    sq.data
from (
    select
        data.*,
        row_number() over (partition by data.device_id order by data.time desc) as rnk
    from
        data
) sq
where
    sq.rnk = 1

The row_number() window function first ranks the rows in the data table on the basis of the device_id and time columns, and the outer query then picks the highest-ranked rows.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spring Data - get last record from the table

From Dev

MYSQL: Retrievieng the last record from each group and combining with the data of another table

From Dev

Select only first or last value for each unique value in data table?

From Dev

SELECT Last record in a Month based on Users table

From Dev

How to select last record by date in a joined table

From Dev

Select record from table with not in other table

From Dev

Select record from table with not in other table

From Dev

Select from table if other table points to record

From Dev

Select data from table

From Dev

Select data from table

From Dev

ResultSet returns only the last record from the table

From Dev

Get Last Five Record From Table

From Dev

Can we increment each record in a table using one of the records from the last row?

From Dev

select top 1 record for each record on 1 table

From Dev

How do I copy data from each record of one table into multiple records of another table related to the first table?

From Dev

Select from joined table only if record exists

From Dev

Select min date record from duplicates in table

From Dev

select record from joined table if it exists

From Dev

Select a particular record from a table using checkboxes

From Dev

select latest duplicate record from table MySQL

From Dev

Select a specific staging record from sybase table

From Dev

Error in select record from sqlite table

From Dev

select obsservableArray record from table in knockout

From Dev

SQLite: How to SELECT "most recent record for each user" from single table with composite key?

From Dev

Select last record of each person

From Dev

MySQL select last row of each one user from a list of users in a table

From Dev

Doctrine - select last 4 rows from table

From Dev

Select last low value from a table

From Dev

Doctrine - select last 4 rows from table

Related Related

  1. 1

    Spring Data - get last record from the table

  2. 2

    MYSQL: Retrievieng the last record from each group and combining with the data of another table

  3. 3

    Select only first or last value for each unique value in data table?

  4. 4

    SELECT Last record in a Month based on Users table

  5. 5

    How to select last record by date in a joined table

  6. 6

    Select record from table with not in other table

  7. 7

    Select record from table with not in other table

  8. 8

    Select from table if other table points to record

  9. 9

    Select data from table

  10. 10

    Select data from table

  11. 11

    ResultSet returns only the last record from the table

  12. 12

    Get Last Five Record From Table

  13. 13

    Can we increment each record in a table using one of the records from the last row?

  14. 14

    select top 1 record for each record on 1 table

  15. 15

    How do I copy data from each record of one table into multiple records of another table related to the first table?

  16. 16

    Select from joined table only if record exists

  17. 17

    Select min date record from duplicates in table

  18. 18

    select record from joined table if it exists

  19. 19

    Select a particular record from a table using checkboxes

  20. 20

    select latest duplicate record from table MySQL

  21. 21

    Select a specific staging record from sybase table

  22. 22

    Error in select record from sqlite table

  23. 23

    select obsservableArray record from table in knockout

  24. 24

    SQLite: How to SELECT "most recent record for each user" from single table with composite key?

  25. 25

    Select last record of each person

  26. 26

    MySQL select last row of each one user from a list of users in a table

  27. 27

    Doctrine - select last 4 rows from table

  28. 28

    Select last low value from a table

  29. 29

    Doctrine - select last 4 rows from table

HotTag

Archive