You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by "cryptoe (via GitHub)" <gi...@apache.org> on 2026/07/14 21:32:19 UTC

[PR] Updated query to matches the index for retriving unused intervals. (druid)

cryptoe opened a new pull request, #19685:
URL: https://github.com/apache/druid/pull/19685

   We create an index like this :
         
   https://github.com/implydata/druid/blob/528e8079cdc92a1c98bc46ea8e0af30032018b3a/server/src/main/java/org/apache/druid/metadata/SQLMetadataConnector.java#L397-L406
    
    Updating query to match the index above. 
    
    
    Real world results on mysql 8.0.0:
    
   ### Before
   ```
   mysql> explain SELECT start, end FROM druid_segments WHERE dataSource = 'xxxx' AND used = false GROUP BY start , end limit 100 \G
   *************************** 1. row ***************************
              id: 1
     select_type: SIMPLE
           table: druid_segments
      partitions: NULL
            type: ref
   possible_keys: idx_druid_segments_used,idx_druid_segments_datasource_used_end_start,idx_druid_segments_datasource_upgraded_from_segment_id
             key: idx_druid_segments_used
         key_len: 1
             ref: const
            rows: xxxx
        filtered: 50.00
           Extra: Using where; Using temporary
   1 row in set, 1 warning (0.01 sec)     
   ````
   As you can see the index used was `idx_druid_segments_used`
   
   ### After
   
    
    ```
    mysql> explain SELECT start, end FROM druid_segments WHERE dataSource = 'xxxx' AND used = false GROUP BY end , start limit 100 \G
   *************************** 1. row ***************************
              id: 1
     select_type: SIMPLE
           table: druid_segments
      partitions: NULL
            type: range
   possible_keys: idx_druid_segments_used,idx_druid_segments_datasource_used_end_start,idx_druid_segments_datasource_upgraded_from_segment_id
             key: idx_druid_segments_datasource_used_end_start
         key_len: xxx
             ref: NULL
            rows: xxx
        filtered: 100.00
           Extra: Using where; Using index
   1 row in set, 1 warning (0.00 sec)
   ```
   As you can see the index used is `idx_druid_segments_datasource_used_end_start`
   
   Will send in another PR to adjust the limit to the intervals as well. 
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] minor: Updated query to matches the index for retriving unused intervals. (druid)

Posted by "cryptoe (via GitHub)" <gi...@apache.org>.
cryptoe commented on PR #19685:
URL: https://github.com/apache/druid/pull/19685#issuecomment-4981002972

   This sounds like a even better fix. Thanks @kfaraz for figuring this out. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] minor: Updated query to matches the index for retriving unused intervals. (druid)

Posted by "kfaraz (via GitHub)" <gi...@apache.org>.
kfaraz commented on PR #19685:
URL: https://github.com/apache/druid/pull/19685#issuecomment-4980935017

   A query like the following would help though:
   
   ```sql
   SELECT `start`, `end`
     FROM (                                                                                                                   
         SELECT `start`, `end`
         FROM druid_segments
         WHERE dataSource = 'transactions'
         AND used = FALSE                                                                                                     
         LIMIT 1000000
     ) AS unused_segments
     GROUP BY `start`, `end`
     LIMIT 10000
   ```
   
   Here, we limit the rows scanned for that datasource to 1M (which affects perf)
   and limit the final result to 10k rows (which only affects the size of the results streamed back to the client).
   
   This query gives me consistently good perf of ~2s on the same data.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] minor: Updated query to matches the index for retriving unused intervals. (druid)

Posted by "cryptoe (via GitHub)" <gi...@apache.org>.
cryptoe commented on PR #19685:
URL: https://github.com/apache/druid/pull/19685#issuecomment-4979915824

   Thanks for the review @jtuglu1 , @kfaraz , @capistrant 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] minor: Updated query to matches the index for retriving unused intervals. (druid)

Posted by "cryptoe (via GitHub)" <gi...@apache.org>.
cryptoe merged PR #19685:
URL: https://github.com/apache/druid/pull/19685


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] minor: Updated query to matches the index for retriving unused intervals. (druid)

Posted by "cryptoe (via GitHub)" <gi...@apache.org>.
cryptoe commented on code in PR #19685:
URL: https://github.com/apache/druid/pull/19685#discussion_r3586676063


##########
server/src/main/java/org/apache/druid/metadata/SqlSegmentsMetadataQuery.java:
##########
@@ -1069,14 +1069,14 @@ public List<Interval> retrieveUnusedSegmentIntervals(
    * guarantee on the order of intervals in the list or on whether the limited
    * list contains the earliest or latest intervals present in the datasource.
    *
-   * @return List of unused segment intervals containing upto {@code limit} entries.
+   * @return List of unused segment intervals containing upto {@code limit} interval entries.
    */
   public List<Interval> retrieveUnusedSegmentIntervals(String dataSource, int limit)
   {
     final String sql = StringUtils.format(
         "SELECT start, %2$send%2$s FROM %1$s"
         + " WHERE dataSource = :dataSource AND used = false"
-        + " GROUP BY start, %2$send%2$s"
+        + " GROUP BY %2$send%2$s, start"

Review Comment:
   +1 @kfaraz comment. 
   We should not assume any ordering gurantees. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] minor: Updated query to matches the index for retriving unused intervals. (druid)

Posted by "kfaraz (via GitHub)" <gi...@apache.org>.
kfaraz commented on code in PR #19685:
URL: https://github.com/apache/druid/pull/19685#discussion_r3584306852


##########
server/src/main/java/org/apache/druid/metadata/SqlSegmentsMetadataQuery.java:
##########
@@ -1069,14 +1069,14 @@ public List<Interval> retrieveUnusedSegmentIntervals(
    * guarantee on the order of intervals in the list or on whether the limited
    * list contains the earliest or latest intervals present in the datasource.
    *
-   * @return List of unused segment intervals containing upto {@code limit} entries.
+   * @return List of unused segment intervals containing upto {@code limit} interval entries.
    */
   public List<Interval> retrieveUnusedSegmentIntervals(String dataSource, int limit)
   {
     final String sql = StringUtils.format(
         "SELECT start, %2$send%2$s FROM %1$s"
         + " WHERE dataSource = :dataSource AND used = false"
-        + " GROUP BY start, %2$send%2$s"
+        + " GROUP BY %2$send%2$s, start"

Review Comment:
   Caller typically should not assume any order of the output as the method does not guarantee any order anyway.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] minor: Updated query to matches the index for retriving unused intervals. (druid)

Posted by "capistrant (via GitHub)" <gi...@apache.org>.
capistrant commented on code in PR #19685:
URL: https://github.com/apache/druid/pull/19685#discussion_r3583275900


##########
server/src/main/java/org/apache/druid/metadata/SqlSegmentsMetadataQuery.java:
##########
@@ -1069,14 +1069,14 @@ public List<Interval> retrieveUnusedSegmentIntervals(
    * guarantee on the order of intervals in the list or on whether the limited
    * list contains the earliest or latest intervals present in the datasource.
    *
-   * @return List of unused segment intervals containing upto {@code limit} entries.
+   * @return List of unused segment intervals containing upto {@code limit} interval entries.
    */
   public List<Interval> retrieveUnusedSegmentIntervals(String dataSource, int limit)
   {
     final String sql = StringUtils.format(
         "SELECT start, %2$send%2$s FROM %1$s"
         + " WHERE dataSource = :dataSource AND used = false"
-        + " GROUP BY start, %2$send%2$s"
+        + " GROUP BY %2$send%2$s, start"

Review Comment:
   nit: may want an inline comment that explicitly states that the group by ordering is being done so we select an index to avoid regression in future (unlikely but possible)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] minor: Updated query to matches the index for retriving unused intervals. (druid)

Posted by "cryptoe (via GitHub)" <gi...@apache.org>.
cryptoe commented on code in PR #19685:
URL: https://github.com/apache/druid/pull/19685#discussion_r3586676063


##########
server/src/main/java/org/apache/druid/metadata/SqlSegmentsMetadataQuery.java:
##########
@@ -1069,14 +1069,14 @@ public List<Interval> retrieveUnusedSegmentIntervals(
    * guarantee on the order of intervals in the list or on whether the limited
    * list contains the earliest or latest intervals present in the datasource.
    *
-   * @return List of unused segment intervals containing upto {@code limit} entries.
+   * @return List of unused segment intervals containing upto {@code limit} interval entries.
    */
   public List<Interval> retrieveUnusedSegmentIntervals(String dataSource, int limit)
   {
     final String sql = StringUtils.format(
         "SELECT start, %2$send%2$s FROM %1$s"
         + " WHERE dataSource = :dataSource AND used = false"
-        + " GROUP BY start, %2$send%2$s"
+        + " GROUP BY %2$send%2$s, start"

Review Comment:
   +1 @kfaraz comment. 
   We should not assume any ordering guarantees. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] minor: Updated query to matches the index for retriving unused intervals. (druid)

Posted by "cryptoe (via GitHub)" <gi...@apache.org>.
cryptoe commented on PR #19685:
URL: https://github.com/apache/druid/pull/19685#issuecomment-4979913234

   I saw some improvements if I reduce the limit of number of intervals from 10000 to 100 which I feel would also improve this query in general and hit the indexes since the number of rows scans would reduce with a data source filters. 
   This is all on mysql 8.0 though. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] minor: Updated query to matches the index for retriving unused intervals. (druid)

Posted by "jtuglu1 (via GitHub)" <gi...@apache.org>.
jtuglu1 commented on code in PR #19685:
URL: https://github.com/apache/druid/pull/19685#discussion_r3584319369


##########
server/src/main/java/org/apache/druid/metadata/SqlSegmentsMetadataQuery.java:
##########
@@ -1069,14 +1069,14 @@ public List<Interval> retrieveUnusedSegmentIntervals(
    * guarantee on the order of intervals in the list or on whether the limited
    * list contains the earliest or latest intervals present in the datasource.
    *
-   * @return List of unused segment intervals containing upto {@code limit} entries.
+   * @return List of unused segment intervals containing upto {@code limit} interval entries.
    */
   public List<Interval> retrieveUnusedSegmentIntervals(String dataSource, int limit)
   {
     final String sql = StringUtils.format(
         "SELECT start, %2$send%2$s FROM %1$s"
         + " WHERE dataSource = :dataSource AND used = false"
-        + " GROUP BY start, %2$send%2$s"
+        + " GROUP BY %2$send%2$s, start"

Review Comment:
   The reason I mentioned it was for safety, since in [this](https://github.com/apache/druid/pull/19645) I attempted doing something similar (on a different query though) and saw perf gains as well.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] minor: Updated query to matches the index for retriving unused intervals. (druid)

Posted by "cryptoe (via GitHub)" <gi...@apache.org>.
cryptoe commented on code in PR #19685:
URL: https://github.com/apache/druid/pull/19685#discussion_r3586676063


##########
server/src/main/java/org/apache/druid/metadata/SqlSegmentsMetadataQuery.java:
##########
@@ -1069,14 +1069,14 @@ public List<Interval> retrieveUnusedSegmentIntervals(
    * guarantee on the order of intervals in the list or on whether the limited
    * list contains the earliest or latest intervals present in the datasource.
    *
-   * @return List of unused segment intervals containing upto {@code limit} entries.
+   * @return List of unused segment intervals containing upto {@code limit} interval entries.
    */
   public List<Interval> retrieveUnusedSegmentIntervals(String dataSource, int limit)
   {
     final String sql = StringUtils.format(
         "SELECT start, %2$send%2$s FROM %1$s"
         + " WHERE dataSource = :dataSource AND used = false"
-        + " GROUP BY start, %2$send%2$s"
+        + " GROUP BY %2$send%2$s, start"

Review Comment:
   +1 @kfaraz comment. 
   We should not assume any ordering grantees. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Updated query to matches the index for retriving unused intervals. (druid)

Posted by "jtuglu1 (via GitHub)" <gi...@apache.org>.
jtuglu1 commented on code in PR #19685:
URL: https://github.com/apache/druid/pull/19685#discussion_r3582852301


##########
server/src/main/java/org/apache/druid/metadata/SqlSegmentsMetadataQuery.java:
##########
@@ -1069,14 +1069,14 @@ public List<Interval> retrieveUnusedSegmentIntervals(
    * guarantee on the order of intervals in the list or on whether the limited
    * list contains the earliest or latest intervals present in the datasource.
    *
-   * @return List of unused segment intervals containing upto {@code limit} entries.
+   * @return List of unused segment intervals containing upto {@code limit} interval entries.
    */
   public List<Interval> retrieveUnusedSegmentIntervals(String dataSource, int limit)
   {
     final String sql = StringUtils.format(
         "SELECT start, %2$send%2$s FROM %1$s"
         + " WHERE dataSource = :dataSource AND used = false"
-        + " GROUP BY start, %2$send%2$s"
+        + " GROUP BY %2$send%2$s, start"

Review Comment:
   could this implicitly change any ordering assumptions of the output?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] minor: Updated query to matches the index for retriving unused intervals. (druid)

Posted by "kfaraz (via GitHub)" <gi...@apache.org>.
kfaraz commented on PR #19685:
URL: https://github.com/apache/druid/pull/19685#issuecomment-4980740267

   > I saw some improvements if I reduce the limit of number of intervals from 10000 to 100 which I feel would also improve this query in general and hit the indexes since the number of rows scans would reduce with a data source filters.
   This is all on mysql 8.0 though.
   
   Yeah, please hold off on reducing the limit, as it will drastically reduce the efficiency of the kill duty.
   
   I have been running the queries on MySQL 8 (with GROUP BY columns reversed and changing limits) on a druid_segments table with more than 15M unused segments for a single datasource and the results for that datasource have been consistently the same irrespective of the query (i.e. the GROUP BY order and the limit does not really affect the query perf).
   I got the same results as you with the EXPLAIN query, but the actual SELECT query took the same time.
   
   > the number of rows scans would reduce with a data source filters
   
   Yes, but changing the limit will not affect this.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org