Monday, September 3, 2012

NugGet Perf, Part V–Searching Packages

NugGet Perf, Part V–Searching Packages:
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.
image
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:
image
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:
image
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:
image
So I am pretty happy about this so far, but we can probably do better. We will see how in our next post.


DIGITAL JUICE

No comments:

Post a Comment

Thank's!