Is it better to filter a resultset using a WHERE clause or using application code?

后端 未结 3 424
面向向阳花
面向向阳花 2021-01-02 06:52

OK, here is a simple abstraction of the problem:

2 variables(male_users and female_users) to store 2 groups of user i.e. male and female

  1. 1 way is to us
3条回答
  •  独厮守ぢ
    2021-01-02 07:53

    If you have 1 million users, do you prefer (considering half of them is male, and half of the is female) :

    • fetching 1 million users from the DB ?
    • or only fetching 500k users from the DB ?

    I suppose you will answer saying you prefer to fetch only half the users ;-) And, depending on the condition, if more complex, it could be even less than this.


    Basically, fetching less data means :

    • less network used "for nothing" (i.e. to fetch data that will immediatly be discarded)
    • less memory used, especially on the PHP server
    • potentially less disk access on the MySQL server -- as there is less data to fetch from disk

    In general cases, we try to avoid fetching more data that necessary ; i.e. we place the filtering on the database side.


    Of course, this means you'll have to think about the indexes you'll place on your database's tables : they'll have to fit the needs of the queries you'll be doing.

提交回复
热议问题