You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by gi...@git.apache.org on 2017/04/07 00:51:57 UTC

[GitHub] tonysun83 commented on a change in pull request #469: Choose index based on fields match

tonysun83 commented on a change in pull request #469: Choose index based on fields match
URL: https://github.com/apache/couchdb/pull/469#discussion_r110298507
 
 

 ##########
 File path: src/mango/src/mango_cursor_view.erl
 ##########
 @@ -135,14 +139,13 @@ composite_prefix([Col | Rest], Ranges) ->
 % reduce view read on each index with the ranges to find
 % the one that has the fewest number of rows or something.
 choose_best_index(_DbName, IndexRanges) ->
-    Cmp = fun({A1, A2}, {B1, B2}) ->
-        case length(A2) - length(B2) of
+    Cmp = fun({IdxA, PrefixA, ColPrefixLengthA}, {IdxB, PrefixB, ColPrefixLengthB}) ->
+        case ColPrefixLengthA - ColPrefixLengthB of
             N when N < 0 -> true;
             N when N == 0 ->
-                % This is a really bad sort and will end
-                % up preferring indices based on the
-                % (dbname, ddocid, view_name) triple
-                A1 =< B1;
+                % We have no other way to choose, so at this point 
+                % select index based on (dbname, ddocid, view_name) triple
+                IdxA =< IdxB;
 
 Review comment:
   So with the old code (which is also what you're leaving with here), I ran a simple test, and here's what happens:
   
   IdxA: [a,b,c]
   IdxB: [a,d,e]
   
   I insert two docs, {"a":1, "b":1, "c":1} and {"a":1000, "d" : 1000, "e": 1000}
   
   then I run the query with _explain:
   
   ```
   {
     "selector": {
       "a": {
         "$gt": 0
       },
       "b": {
         "$gt": 0
       },
       "c": {
         "$gt": 0
       }
     }
   }
   ```
   and I get back IdxB
   ```
   "index": {
           "ddoc": "_design/ccf468609d8ca7ef1744797c6b685d82cb552335",
           "name": "IdxB",
           "type": "json",
           "def": {
               "fields": [
                   {
                       "a": "asc"
                   },
                   {
                       "d": "asc"
                   },
                   {
                       "e": "asc"
                   }
               ]
           }
       }
   ```
   I think we've been wrong all this time, and it should be IdxA right?
   Can you try out this scenario in the tests? We might have to change this comparison. 
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services