Are sessions faster than database queries?

So, for example, the user logs into the system, and the system stores information about them, for example: birth date , so is it faster to get this information from the session or to request a database for it?

My idea was that the user only needs to log in once, and the session always exists, but if I query the database, then if the user reloads the page, the system should query again and again, instead of receiving data from a temporary "places".

I use PHP and MySQL.

+7
performance database login session temporary
source share
3 answers

For almost any language and database, yes. A user session is usually simply stored in memory, and getting hold of it is just a matter of search. Database access is usually associated with some sort of socket connection with another process. Relatively hard.

+9
source share

How do you do all this? The way to do this is to verify the user credentials on the login page, and yes, there you need to make a request to check if the user meets the criteria criteria in the database. If so, you store them in the session, and then continue to work on that session.

So this is not a comparison, you should query the database once on the login page and use the session afterwards.

Login page

 // database query run once only at this page // if user exits, you store it into session else redirect with an error message 
+1
source share

Actually, Karl’s answer is not entirely correct, and saying “MySql” doesn’t help either.

You see that database systems like mysql have "storage mechanisms". Usually they are written to files, but there are records that are written to memory (MEMORY), others write memory, but save a backup copy of the file (MyISAM) and a few - / dev / null (BLACKHOLE).

So, it all depends on the storage mechanism:

  • MyISAM - default engine with MySQL 3.23 with excellent performance
  • MEMORY - A hash stored in memory, useful for temporary tables
  • InnoDB - supports transactions, row level locking and foreign keys.
  • BerkeleyDB - supports transaction and page-level locking
  • BLACKHOLE - / dev / null storage engine (everything you write to it disappears)
  • EXAMPLE - An example of a storage engine
  • ARCHIVE - Archival storage engine
  • CSV - CSV Storage
  • ndbcluster - clustered, fault-tolerant memory-based tables
  • FEDERATED - Federated MySQL storage engine
  • MRG_MYISAM - collection of identical MyISAM tables
  • ISAM - Deprecated Storage Engine

(list from PhpMyAdmin Egines list)

0
source share

All Articles