Adrian Tanase
Adrian Tanase

Reputation: 700

MySql optimization / speed that's been haunting me

Is there a difference between

SELECT * FROM my_table

and

SELECT my_column_id FROM my_table

where:

  1. my_table has million(s) of rows
  2. there are a lot of concurrent users doing sql queries on the website

in speed? Is it better to SELECT just 1 column instead of * (all) for a rowCount() query?

Upvotes: 0

Views: 70

Answers (1)

George
George

Reputation: 36784

Of course theres a difference.

SELECT * FROM my_table - Select all column's worth of data (whole table).

SELECT my_column_id FROM my_table - Select one column's worth of data.

Its common sense really.. More data.. More time.

But @Juhana makes a good point. If your just counting rows, use SELECT COUNT(*)

For the sake of your comment....

O--O-------------O-------------O
|ID|   my_col_1  |   my_col_2  |
O--O-------------O-------------O
|0 | fskdjfh     | jfkhgdkfj   |
|1 | NULL        | hfkjsdh     |
|2 | jfkdhsdkjh  | NULL        |
|3 | fdfhkjh     | NULL        |
|4 | NULL        | NULL        |
O--O-------------O-------------O

SELECT COUNT(my_col_1) will return 3

SELECT COUNT(my_col_2) will return 2

SELECT COUNT(*) will return 4

Upvotes: 2

Related Questions