views:

68

answers:

3

So for example, the user is logging in, and the system is storing informations about them example: birth date, so is faster to get this information from the session, or to query the database for it?

My idea was, that the user needs to login just once and the session is always there, but If I query the database, then if the user reloads the page, the system needs to query again and again, instead of getting the data from a temporary 'place'.

I use PHP and MySQL.

+6  A: 

For almost any language and database, yes. A user session is usually just stored in memory, and getting a hold of it is just a matter of looking it up. A database access usually involves some socket communication with a different process. Rather heavy in comparison.

Carl Smotricz
As far as I know, sessions in PHP are stored in files by default, not in memory. For each hit this session file has to be read from filesystem and parsed into PHP array.You're right, session should be faster but the reason is - you have to load user's session data for other reasons and the birtday he's talking about will be already there - no need to ask the database.
dwich
+1  A: 

How are you doing it all? The way to do is to check the user credentials at the login page and yes there you do have to make a query to check if the user specified criteria match in the database. If they do, you store them in session and then you continue based on that session.

So, it is not about comparison, you have to make to query the database once at the login page and use 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
Sarfraz
I know how to do it, my question was is faster than database query or not :)
CIRK
@CIRK: Of course, session is faster but then you have to account for security as well.
Sarfraz
Thanks security is a whole new topic, thanks for saying it, I created a new question check it out:http://stackoverflow.com/questions/3224286/php-what-are-the-risks-of-php-sessions
CIRK
+1  A: 

Actually, Carl's answer is not fully correct, and saying "MySql" doesn't help either.

You see, database systems like mysql have "storage engines". These usually write to files, but there are some which write to memory (MEMORY), others write memory but keep a file backup (MyISAM) and a few to /dev/null (BLACKHOLE).

So it all depends on the storage engine:

  • MyISAM - Default engine as of MySQL 3.23 with great performance
  • MEMORY - Hash based, stored in memory, useful for temporary tables
  • InnoDB - Supports transactions, row-level locking, and foreign keys
  • BerkeleyDB - Supports transactions and page-level locking
  • BLACKHOLE - /dev/null storage engine (anything you write to it disappears)
  • EXAMPLE - Example storage engine
  • ARCHIVE - Archive storage engine
  • CSV - CSV storage engine
  • ndbcluster - Clustered, fault-tolerant, memory-based tables
  • FEDERATED - Federated MySQL storage engine
  • MRG_MYISAM - Collection of identical MyISAM tables
  • ISAM - Obsolete storage engine

(list from PhpMyAdmin Egines list)

Christian Sciberras
so if I use `MEMORY`, it will work like the `SESSION`?
CIRK