php: sessions vs. database

后端 未结 3 802
太阳男子
太阳男子 2021-01-05 05:44

I have a class that retrieves its memeber (more or less 10 members) from a database.

My question is: is it more efficient to fetch them every time from the db (MySQL

相关标签:
3条回答
  • 2021-01-05 06:07

    Whether you have 100,000 rows or 10,000,000 rows in your table is irrelevant if you have your indexes setup properly.

    Having said that PHP's default file based session is definitely faster than retrieving from a database, but you won't notice the difference until your site is very heavily used, like 5,000+ queries per second.

    0 讨论(0)
  • 2021-01-05 06:12

    Depends what you mean by 'efficient'. One's time-efficient, one's disk-space-efficient. And it's very difficult to judge the two against each other, unless your requirements are at either extreme end of the spectrum. You'd probably do OK just flipping a coin, measuring performance over time, and adjusting based on any observed problems.

    Premature optimisation is the root of all evil.

    0 讨论(0)
  • 2021-01-05 06:19

    Considering you're storing an ID in the session anyway, the session makes the most sense. Doing a session_start() loads your session information so whether you've loaded 1 or 10 items after that is largely irrelevant (unless they're really large but that'll be a problem in any case).

    So stick with the session.

    If you get really concerned about speed use an in-memory cache like APC or memcache. Worrying about speed for 10 items from the filesystem or database is a distraction. The difference is going to be so minimal as to be irrelevant.

    Note: the above assumes two things:

    1. The query is performant (retrieving 10 rows out of 100k should be doable in under 0.1 seconds); and
    2. You are doing one query not 10.
    0 讨论(0)
提交回复
热议问题