NugGet Perf, Part V–Searching Packages
Join the DZone community and get the full member experience.Join For Free
now we get to the good parts, actually doing searches for packages, not just showing them in packages page, but doing complex and interesting searches. the current (after optimization) query looks like this:
select top (30) -- fields removed for brevity from ( select filtered.id , filtered.packageregistrationkey , filtered.version , filtered.downloadcount , row_number() over (order by filtered.downloadcount desc, filtered.id asc) as [row_number] from ( select packageregistrations.id , packages.packageregistrationkey , packages.version , packageregistrations.downloadcount from packages inner join packageregistrations on packageregistrations.[key] = packages.packageregistrationkey where ((((packages.isprerelease <> cast(1 as bit))))) ((((and packages.islateststable = 1)))) ((((and packages.islatest = 1)))) and ( packageregistrations.id like '%jquery%' escape n'~' or packageregistrations.id like '%ui%' escape n'~' or packages.title like '%jquery%' escape n'~' or packages.title like '%ui%' escape n'~' or packages.tags like '%jquery%' escape n'~' or packages.tags like '%ui%' escape n'~' ) ) filtered ) paged inner join packageregistrations on packageregistrations.[key] = paged.packageregistrationkey inner join packages on packages.packageregistrationkey = paged.packageregistrationkey and packages.version = paged.version where paged.[row_number] > 30 order by packageregistrations.downloadcount desc , paged.id
i can hear the db whimpering in fear in a dark corner, where it is hiding while it isn’t being flogged by cruel and unusual queries.
okay, there is a certain amount of hyperbole here, i’ll admit .but at least it is funny.
at any rate, here we have query that allows the user to search for the latest stable packages by their id, title or tags. to make things interesting for the db, all queries are using ‘%jquery%’ form. this is something that particularly every single resource you can find about databases will warn you against. you can read why here . i think we can safely assume that the nuget guys do not use ef prof , or they wouldn’t go this route.
actually, i am being unfair here. there really aren’t many other good options when you start to need those sort of things. yes, i know of sql server full text indexes, they are complex to setup and maintain and they don’t provide enough facilities to do interesting stuff. they are also more complex to program against. you could maintain your own indexes on the side (lucene, fast, etc). now you have triple the amount of work that you have to do, and care and maintenance of those isn’t trivial. for either the devs or the ops team.
so i can certainly follow why the decision was make to use like ‘%jquery%’, even though it is a well known problem.
that said, it is the wrong tool for the job, and i think that ravendb can do a lot more and in more interesting ways as well.
let us see the index that can handle these sort of queries.
what does this index do?
well, it index the a bunch of fields to allow them to be searched for by value, but it also do something else that is query interesting. the query field in the index takes information from several different fields that are all indexed as one. we also specify that this index will treat the query field as the target for full text analysis. this means that we can now write the following query:
in code, this would look like this:
var results = session.query<package_search.request, package_search>() .where(x=> x.islatestversion && x.isabsolutelatestversion && x.isprerelease == false) .search(x=>x.query, usersearchterms) .orderbydescending(x=>x.downloadcount).thenby(x=>x.created) .take(30 .as<package>() .tolist();
this will generate the query you can see above, and return the first 30 results.
but a lot more is actually happening here, let us look at what actually goes on in the index:
here you can see the actual terms that were indexed in the database for each of the documents. the reason that this is important is that when it comes the time to do searches, we aren’t going to need to do anything as crass as a full table scan, which is what sql has to do. instead, all of those terms are located in an index, and we have the <<jquery ui>> search string. we can them do a very simple index lookup (cost of that is o(logn), if you’ll recall) to find your results.
and of course, we have this guy:
so i am pretty happy about this so far, but we can probably do better. we will see how in our next post.
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.