Sunday, October 26, 2008

Basics - 3

The greatest error you can make is to forget your earliest lessons.

I'm not referring to forgetting the detail - I write code most days and I haven't written a fundamental algorithm method/function/predicate in years, but the detail can be the thing that holds us back and isn't relevant here.

Whether you're a system admin, software developer or some other form of IT slave, you are dealing with other people's software. Even if you're an OS/compiler/VM/embedded developer, you are dealing with other people's code. Given such preconditions, none of us are special - we've all been taught the same basic lessons (we hope - surely no one is using a linear search in their performance-critical code...oh wait....).

Concepts are the critical thing here - applying operations on any form of efficient data structure tends to eliminate said efficenices, remembering that sorts depend on data size, that searches depend on the structure of the input data and minimising structure size is always in your best interest unless there is some other imperitive that is far more pressing (of which I'm yet to see an example).

Why then I still see queries like this in production software is beyond me:

SELECT * FROM some_table AS t1 INNER JOIN some_other_table AS t2 ON RIGHT(t1.field1, 3) = RIGHT(t2.field1, 3) WHERE LEFT(t1.field2, 3) = 'XYZ' AND LEFT(t2.field3, 3) = 'ABC') ORDER BY t1.field4 ASC LIMIT 0,2001

(No, it's not a MySQL or PostgreSQL database - MySQL would not cope given it's 1-index-per-table-in-most-cases evaluation and PostgreSQL would have filled its logs with rewrite-your-query-already-you-idiot - the query has been recomposed to protect the guilty).

As soon as I spoke to the person who wrote this query, they were very interested in what they did wrong and how to fix the problem (even though no performance isues were evident). Since then they have always checked their work and have done the perfectly right thing in terms of getting queries, code and other artefacts dead right.

Next time you're called a geek by your fellow code warriors or system grunts, remember that you're probably going to be the person who finds the cause of the next big issue. We're all taught the fundamentals early on for a reason. The fact that so many engineering and mathematics students perform poorly on basic operations of addition and multiplication (and their associated inverses) betrays the problem seen in industry here - don't move with the herd.






No comments: