RasaHQ / rasa

💬 Open source machine learning framework to automate text- and voice-based conversations: NLU, dialogue management, connect to Slack, Facebook, and more - Create chatbots and voice assistants
https://rasa.com/docs/rasa/
Apache License 2.0
18.32k stars 4.57k forks source link

SQL event.timestamp is not always unique #13019

Closed vcidst closed 4 months ago

vcidst commented 4 months ago

Proposed changes:

While the original query is quite complicated,

SELECT *
FROM events
WHERE sender_id = :sender_id
  AND (
    timestamp >= (
      SELECT max(events.timestamp) AS session_start
      FROM events
      WHERE events.sender_id = :sender_id
        AND events.type_name = :type_name
    )
    OR (
      (
        SELECT max(events.timestamp) AS session_start
        FROM events
        WHERE events.sender_id = :sender_id
          AND events.type_name = :type_name
      ) IS NULL
    )
  )
ORDER BY timestamp, id;

I've some quick results on the performance of this query. Order by ID understandably is the best option for performance.

Order by timestamp

zi=# explain select * from events order by timestamp;                                                       QUERY PLAN                             
-------------------------------------------------------------------
 Sort  (cost=544.08..553.94 rows=3945 width=490)
   Sort Key: "timestamp"
   ->  Seq Scan on events  (cost=0.00..308.45 rows=3945 width=490)
(3 rows)

Order by id

----------------------------------------------------------------------------------
 Index Scan using events_pkey on events  (cost=0.29..543.61 rows=10088 width=177)
(1 row)

Order by timestamp and id

zi=# explain select * from events order by timestamp, id;
                             QUERY PLAN                             
--------------------------------------------------------------------
 Sort  (cost=1040.75..1065.97 rows=10088 width=177)
   Sort Key: "timestamp", id
   ->  Seq Scan on events  (cost=0.00..369.88 rows=10088 width=177)
(3 rows)

Status (please check what you already did):

github-actions[bot] commented 4 months ago

🚀 A preview of the docs have been deployed at the following URL: https://13019--rasahq-docs-rasa-v2.netlify.app/docs/rasa