- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Admin
The problem with regexen is a lot like the problem with library functions: you can never be entirely sure what they're doing. So, in cases like this where the results are important, it is safest to write your own.
Admin
If you comment your code you can get a good idea of what the function should be doing. If you didn't get the arcane syntax of a regular expression just right then the next developer will at least know what you are trying to do and make the appropriate adjustments. Sometimes code need to have the bugs removed and the same is true of regular expression. A lot of developers will simply look at the expression guess at what it was trying to do and either replace it with something like this example or somehow work around the problem. Writing code is a lot easier than reading it.
Admin
I'm actually interested in the execution path of the searchstring when the database server is able to parallelize the unions and each of the searchfields are indexed.
Admin
"turning option strict on for a large project"
Ha! There is the TRWTF.
Or maybe its Visual Basic (someone had to say this)
Admin
Some people learn what OR operator does (on certain databases) with indices sooner, some later. Union still has a place in SQL as a poor's man OR.
Admin
in same cases using unions instead of OR really makes a sense - sometimes it's better from performance point of view use unions ....
Admin
Nonody noticed the order by only has an efect on the last select of the unions. i guess that's not the effect the developer was going for
Admin
Hey Chris, sorry for all the injections, but please think a bit and may be you will realize that on old systems UNION worked faster than OR.
captcha valid lol
Admin
Nonody = Nobody
Admin
I think, in the case of SQL Server, that the optimizer really only looks at the first query in UNIONs to do its magic, so I gotta believe that ORs would make for a better query plan - unless the vast majority of the results are caught by the first query. It may be unavoidable, but likely the bigger problem is the LIKE operators.
Admin
Not on SQL Server:
Admin
Most databases have these things called query optimizers these days. Writing an arcane overly complex query to make the computer's work easier is premature optimization, like trying to outsmart your compiler. Don't forget that 99.9% of the ongoing costs are developer maintenance time, not CPU time.
Oh, this is MS SQL?
Never mind.
Admin
Ha!
Admin
"The problem with regexen is a lot like the problem with library functions: you can never be entirely sure what they're doing. So, in cases like this where the results are important, it is safest to write your own. "
That sounds like the "Not Invented Here" argument to me.
Admin
"regex scares me a little, OK, alot actually"
Aaaaargh! It's "a lot" - two words. You wouldn't write "afew", would you? Then again, it's probably better than writing "allot" (when meaning "a lot", not "share out"). </petpeeve>
Admin
The OR solution would have different results in cases where a record matches multiple search criteria - in that case, the current solution would include duplicate rows.
I'm not entirely sure why returning duplicate rows would be desired, but that's a different WTF.
Admin
So by this logic the entire application framework and operating system would have to be replaced. And the hardware, and the drivers.
Or you could write some unit tests on the regex expression that validate what it's doing.
Admin
Actually, here UNION is the same as OR, but in the MySQL point of view, presents a better performance ;)
Admin
In many cases it is safest NOT to write your own! EG., encryption. Or did I just win a "whooosh"?
Did he really have to use charAt for two linear ranges? I think that if (c >= 'a' && c <= 'z') is less error-prone than "abcdefghijklmnpqrstuvwxyz".chartAt(c) != -1 Quick, spot the error!
Admin
TRWTF is to say that regular expressions are tidy and save processing power :-)))
Admin
Admin
Need also to fix incorrect use of non-existent words.
http://www.grammar-monster.com/easily_confused/alot_a_lot_allot.htm
Admin
Admin
Admin
Dave's not here, man.
Admin
As for hardware, I suppose you're still buying yours from China? Hmmm?
If you're in a situation where results matter, safest bet is to make your own. If you want it done right, do it yourself.
You know how free trade is always destroying local economies? Buy from your own town, not online? Well get more local. Only trade with people on your own block. In your own house. Your own bedroom. See where this is going?
Admin
TRWTF is Whitesmiths braces, amiright. Allman or bust, that's what I say.
Admin
How does it come that you do not know what's the num variable for? Has following function been deleted:
Admin
At least it came with a disk driver. But I had to add a layer on top of that so that a single file could be larger than a whole floppy disk.
It's not that hard. You can do it if you want.
Admin
"^\w*$"
Simple enough.
Admin
Admin
It's never as simple as it looks.
Admin
Because there will be a time when it's a good idea to write more code for simple tasks that have been done a million times over like confirming a string is composed only of the basic Latin alphabet.
When results are important you go with what works rather than ginning up your own solutions with all the inherent human error that new code implies.
Admin
Admin
Admin
Admin
That is problem with query optimize though. If your application needs the performance right now okay, sometimes you have to code around the ugly. In general though I'd say if your database is not stressed and performance is alright, I'd rather right clean code, at least when it ought to be possible for an optimizer to deal with well, than stick in some ugly workaround.
If you have to use multiple selects and UNION to get reasonable execution plans for simple OR conditions, the problem is your database software.
Admin
String searchString="select * from ProjectAndTask_view"; if (searchWhere !=""){ searchString=searchString + " where ProjectCode like '" + searchWhere + "%' union " ; searchString=searchString + "select * from ProjectAndTask_view where ProjectDesc like '" + searchWhere + "%' union " ; searchString=searchString + "select * from ProjectAndTask_view where TaskCode like '" + searchWhere + "%' union " ; searchString=searchString + "select * from ProjectAndTask_view where TaskDesc like '" + searchWhere + "%'" ; } searchString = searchString + " ORDER BY sortval";
Actually, this is very possible. I had todo a similar things for performance issues. A union sometimes will perform aster then combining multiple ORs.
In my case though the query was created at runtime, and it could not be predicted at coding time. The ORsolution was sometimes fast, sometimes very slow. Using the UNION alternative was "not to slow, not to fast" (almost) all of the time.
Admin
It is also faster in MSSQL
Admin
Admin
They usually do save processing over say a mess of split(),join(),mid$(),left$(),right$(),For i = 0 to len(str); if str[i] = .... EndFor
stuff
Admin
TRWTF with the last one is using a boolean variable to indicate that it's time to break out of the loop, instead of using GOTO, like God intended.
Admin
Admin
Admin
Admin
Admin
Admin
You could call it an assembler.
Admin