Kaleb has already given you the solution (and Joel has warned about index performance in the comment) but I'd like to suggest another route, one that will scale well. If your tables are never going to get to be very large, you can ignore this advice. In fact, you can ignore it right up until the time when you expect performance will suffer (or performance has actually started suffering) - this is the YAGNI principle.
Databases are almost always read far more than they're written. This means that the right time to figure out when the URL starts with, or contains, a "/q/" is when the data is put in the table, not every single time you extract it. This amortizes the cost of the calculation (done at write time) across all the reads.
For this scalable solution, you should have a totally separate column in the table, something like "startsWithQ"
or "hasQ"
.
Then, with an insert/update trigger, set that column based on the URL being put into the table.
Then your query will become:
select * from MyURLs where hasQ = 'YES'
and, provided you have an index on that column, your queries will scream along.
If you actually look at most database problems, they're usually "my query isn't fast enough" rather than "my disk isn't big enough". Trading disk space for speed is usually the best approach.
This may provide a performance improvent if your search string is '/q/%'
. This should be profiled, regularly and in production - databases are rarely set-and-forget since performance can change based on the data in the tables - measure, don't guess.
It will certainly provide an improvement if the search string is '%/q/%'
, unless your database is one of those very rare beasts that has more writes than reads.