I have a sql query that I tried executing (below) that took 10 seconds to run, and since it was on a production environment I stopped it just to be sure there is no sql locking going on
SELECT TOP 1000000 *
FROM Table T
Where CONVERT(nvarchar(max), T.Data) like '%SearchPhrase%' --T.Data is initially XML
Now if I add an order by on creation time (which I do not believe is an index), it takes 2 seconds and is done.
SELECT TOP 1000000 *
FROM Table T
Where CONVERT(nvarchar(max), T.Data) like '%SearchPhrase%' --T.Data is initially XML
order by T.CreatedOn asc
Now the kicker is that only about 3000 rows are returned, which tells me that even with the TOP 1000000
it isn't stopping short on which rows it's still going through all the rows.
I have a basic understanding of how SQL server works and how the query parsing works, but I'm just confused as to why the order by makes it so much faster in this situation.
The server being run is SQL server 2008 R2