Need advice: Effenciency, Sesions variables and databases

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Weemen
Forum Newbie
Posts: 3
Joined: Sun Oct 26, 2008 5:58 am

Need advice: Effenciency, Sesions variables and databases

Post by Weemen »

Hey all,

Well I finally found a good php forum wooohoo, I hope you can give me a little feedback on an idea that I have.

I'm looking for an effecient way to make website menu. At the moment I can bring it back to 1 query per page view
cause I've made an extra database table where the menu is sorted out for me which already was a great improvement
with my very first solution. (Aprox 4 per pageview).

I was wondering if the following is possible. I'm thinking to store the tree at my session variable. In such situation I don't
have to do any query at all. I'll update the tree at the moment that I made a change at my CMS. This solution gives me two problems

1) Is it possible to update a session variable (or a least giving a sign to update tree) in all exisisting sessions from a specific domain?
2) Are there any arguments for not choosing this solution?

Maybe an extra question could be, what is in general good to store at a session variable and what is in general bad to store at session
variable. I'll would like to choose for my solution cause I think the session variable is faster then the database but I'm not sure if it's the
smartest idea to do

All ready thanks for reading

Leon
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Need advice: Effenciency, Sesions variables and databases

Post by Christopher »

I don't think there is any rule about what to store in sessions. Obviously it is data that only should exist for a session's length of time. Something like you menu could be built and saved in the session to save the query each request. I don't think there is an easy to update on variable in all sessions.
(#10850)
Weemen
Forum Newbie
Posts: 3
Joined: Sun Oct 26, 2008 5:58 am

Re: Need advice: Effenciency, Sesions variables and databases

Post by Weemen »

Well I might found the answer, I think it's possible to override the storage path of the session files. If I can store them in a local folder then I can change the session files. Not sure if it's works but I'll give it a try :)

The bad is that's a damn dirty trick and if it's go wrong then all my sessions are messed up :)
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Need advice: Effenciency, Sesions variables and databases

Post by allspiritseve »

Weemen wrote:Well I might found the answer, I think it's possible to override the storage path of the session files. If I can store them in a local folder then I can change the session files. Not sure if it's works but I'll give it a try :)

The bad is that's a damn dirty trick and if it's go wrong then all my sessions are messed up :)
You might find it a bit easier to store your sessions in the db, and make modifications on those rows:

http://shiflett.org/articles/storing-se ... a-database

Edit: but... before you go to all the trouble, consider not trying to optimize until you really have performance problems. I find it hard to believe retrieving a simple menu from the db is noticeably slowing your scripts.
Weemen
Forum Newbie
Posts: 3
Joined: Sun Oct 26, 2008 5:58 am

Re: Need advice: Effenciency, Sesions variables and databases

Post by Weemen »

Hey very special thnx for that link. That's very interesting article!!!

For me, my goal is just looking for maximum effeciency. It's not that my scripts are slow :)
I like these troubles, it's gives me a challenge :)

Another thnx for the article :D
Hannes2k
Forum Contributor
Posts: 102
Joined: Fri Oct 24, 2008 12:22 pm

Re: Need advice: Effenciency, Sesions variables and databases

Post by Hannes2k »

Hi,
1) That's normaly not possible. There are some workarounds, but you shouldn't use it.
2) A sql database can perform thousands of queries per second, so one sql query more per page view shouldn't be a problem.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Need advice: Effenciency, Sesions variables and databases

Post by josh »

Session data is saved by PHP as a serialized object by default, also sessions are intended for data specific to a single user. For something site wide like the menu I'd put it in the database and just use a regular SQL query to build an array on each page request
Post Reply