2

I was reading Django Book and came across interesting statement.

Notice that Django doesn’t use SELECT * when looking up data and instead lists
 all fields explicitly. This is by design:
 in certain circumstances SELECT * can be slower,

I got this from http://www.djangobook.com/en/1.0/chapter05/

So my question is can someone explain me why SELECT * can be slower, than call every single column explicitly. Would be good if you can give me some examples. Or if you think the opposite (it doesn't matter), can you explain why?

Update:

That's the table :

BEGIN;
CREATE TABLE "books_publisher" (
    "id" serial NOT NULL PRIMARY KEY,
    "name" varchar(30) NOT NULL,
    "address" varchar(50) NOT NULL,
    "city" varchar(60) NOT NULL,
    "state_province" varchar(30) NOT NULL,
    "country" varchar(50) NOT NULL,
    "website" varchar(200) NOT NULL
);

And that's how Django will call SELECT * FROM book_publisher:

SELECT
    id, name, address, city, state_province, country, website
FROM book_publisher;
Adi
  • 5,089
  • 6
  • 33
  • 47
Vor
  • 33,215
  • 43
  • 135
  • 193
  • 1
    possible duplicate of [Why is SELECT * considered harmful?](http://stackoverflow.com/questions/3639861/why-is-select-considered-harmful) – xdazz Aug 30 '12 at 03:14
  • Very good Link, very very helpful! thank you – Vor Aug 30 '12 at 03:26

4 Answers4

2
  1. performance (will matter only if you are selecting less columns than there are in the table
  2. I am not sure about how Django works; but in some languages/ db drivers "select *" will cause an error if you change the table schema (say add a new column). This is because the DB driver "caches" the table schema and now its internal schema does not match the table schema.
Dharmendar Kumar 'DK'
  • 2,082
  • 17
  • 18
1

If you have 100 columns, SELECT * will return the data for all columns. Listing the columns explicitly will reduce the columns returned, therefore reducing the amount of data transmitted between the server and application.

Joey Gennari
  • 2,361
  • 17
  • 26
0

This is clearly not faster in many case, and when one of them is faster, it is by a slight margin: check by yourself, benchmarking a lot of queries :)

It might be faster to select only some columns in some case, including when you select only column that are on a combined index, avoiding the need to read the whole row, and also when you avoid accessing BLOB or TEXT columns on MySQL.

And naturally if you select less column you will transfer less data between MySQL and your application

Parallelis
  • 699
  • 3
  • 6
  • 1
    Well it is obvious that if I select less column I will transfer less data. But what I'm asking is why Django does not use SELECT * and instead call each column explicitly. And in the book from oficial web site it says that explicit is better than implicit? Better why? – Vor Aug 30 '12 at 03:21
0

I think in this exact case there will be no performance difference, this is exactly that in certain circumstances SELECT * can be slower is all about.

Petr Abdulin
  • 33,883
  • 9
  • 62
  • 96