zucke...@gmail.com wrote:
>> PS. I assume your "select *" (selstar) was for the sake of
>> simplifying the example. Hopefully you realize the potential for
>> problems that can be caused by using selstar in production code.
>
> Thank you everyone for the replies. I'll keep looking in other
> sections of my form to increase speed.
>
> could you explain further about why "Select *" is undesireable?
> Assuming that all of the fields are needed for the recordset purpose.
>
1. As Gene says - self-documentation
2. Defensive programming - using selstar says you are assuming that nobody
will change the structures of your tables in the future, not usually a wise
assumption. If new columns are added, will your code be able to use them or
will the query engine be wasting time and memory returning them needlessly.
Since you're worried about the performance impact of saved vs ad hoc
queries, one can assume you're using the performance boost of referring to
the fields in the recordset by ordinal index number rather than by name.
What happens if someone decides he'd like the fields in all the tables to be
displayed alphabetically? Do you want your code to break just because
someone reorders the columns in the tables?
3. If tables are being joined in the query, selstar guarantees that you will
ge returning the same data multiple times, once for each table in the joins.
Unless it's an outer join you should only be returning the columns used in a
join from one of the tables in the join.Why return ID twice when the two
columns are guaranteed to hold the same data?
4. You're concerned about performance - why make more work for the query
engine? Using selstar forces the engine to access the table twice: once to
get the column names, and then to get the data. Yes, it's not likely to have
a noticeable effect on performance, but it is there. Executing a query from
an external application will exacerbate this.
One time selstar is "acceptable" in production code is in an EXISTS
subquery: WHERE EXISTS (select * from sometable where <some conditions
apply>). Most database engines will optimize that construction
appropriately, given that it's not really returning any data when EXISTS is
used.
Another is when using Count(*) to allow counting rows where columns contain
Nulls. If col contains Nulls in some rows, Count(*) will count all the rows;
Count(col) will only count the rows where col contains non-Null values.