简体   繁体   English

会话变量与Mysql表

[英]Session variables vs. Mysql table

I'm planning on saving my users often used parameters, ie name, picture, etc, in session variables as opposed to pulling then from the MySQL table each time they are needed. 我打算在会话变量中保存我的用户经常使用的参数,即名称,图片等,而不是每次需要时都从MySQL表中提取参数。 Saving often used parameters in variables as opposed to a database in theory should be more efficient, but because I'm not sure how SESSION variables are saved I'm not too sure if this is true. 从理论上讲,与数据库相比,将常用参数保存在变量中应该更有效,但是由于我不确定SESSION变量的保存方式,所以我不太确定这是否成立。 Does anyone know if pulling info. 有谁知道是否提取信息。 from a SESSION variable is more efficient than querying the MySQL table? 从SESSION变量比查询MySQL表更有效?

The term variable is used loosely as SESSION "variables" are stored in files in the server's temporary directory. SESSION“变量”存储在服务器临时目录中的文件中,因此术语“变量”被宽松地使用。

You would think reading files is more costly than reading a database, I mean that is what a database is essentially, a file, but it is optimized for this purpose as opposed to "temporary session files" 您可能会认为读取文件比读取数据库要花费更多,我的意思是数据库本质上是一个文件,但是为此目的而进行了优化,而不是“临时会话文件”

Yes, pulling information from a session variable is more efficient than querying a database for that info. 是的,从会话变量中提取信息比在数据库中查询该信息更为有效。 However, loading the information INTO the session variables requires reading a file off of your servers file system and into RAM, which depending on many factors (disk speed, IO load, db speed, etc) might be slower or faster than reading the same information from a DB. 然而,加载信息存储会话变量需要读取一个文件,把你的服务器的文件系统和到RAM,这取决于很多因素(硬盘速度,IO负载,数据库速度等)可能会更慢或快于读取相同的信息从数据库。 Without information on your specific setup, it's hard to say. 没有有关您的特定设置的信息,这很难说。 One thing to keep in mind, if you plan on growing and using more than one web server, you will need to write some custom session handlers to either store your sessions to a central server (possibly a database), memcache, or a shared mount point where all your web servers can go to fetch the session files. 要记住的一件事是,如果您计划增长并使用多个Web服务器,则需要编写一些自定义会话处理程序以将会话存储到中央服务器(可能是数据库),内存缓存或共享安装指向所有Web服务器可以用来获取会话文件的位置。

In the end, putting something into the session and using it from there can be more efficient than loading it from the DB every time, but you are still loading it from somewhere, and so, knowledge of your hardware and your setup will be your best guide. 最后,将某些内容放入会话中并从中使用它比每次从数据库中加载更有效,但是您仍然从某个位置加载它因此,对硬件和设置的了解将是您最好的选择指南。

The default Session handler for PHP stores that info to disk; PHP的默认Session处理程序将该信息存储到磁盘。 one unique temporary file per session. 每个会话一个唯一的临时文件。 The issues you may come across are if the disk/file system gets overloaded, or if your data becomes stale. 您可能会遇到的问题是,如果磁盘/文件系统过载,或者数据变得陈旧。

If you're making a trip to disk to access the session, there is slightly less overhead than accessing MySQL, but you're still making a trip to disk upon every page request. 如果要访问磁盘以访问会话,则开销要比访问MySQL稍少,但是仍然需要在每次页面请求时进行磁盘访问。 You can try to use an in-memory Session handler. 您可以尝试使用内存中的Session处理程序。

Session variables are preferred for persisting a relatively small amount of temporary data. 最好使用会话变量来保留相对少量的临时数据。 They're good for "sessions". 它们适合“会话”。

Use a database for everything else. 使用数据库进行其他所有操作。 Especially for: 尤其是给:

  • larger amounts of data, 大量的数据,
  • for any kind of "transaction", or 进行任何形式的“交易”,或
  • for data that needs to be persisted between "sessions". 需要在“会话”之间保留的数据。

This article is somewhat dated, and it doesn't apply to PHP per se ... but it should give you some idea about the relative efficiencies of filesystem (eg NTFS) vs database (eg MSSQL): 本文有些过时了,它本身并不适用于PHP ...但是它应该使您对文件系统(例如NTFS)与数据库(例如MSSQL)的相对效率有所了解:

Yes it's more efficient to use session variables. 是的,使用会话变量更有效。 Typically Session variables are stored on the server in the /tmp directory (you can check your PHP Info file to see how yours is configured. 通常,会话变量存储在服务器的/ tmp目录中(您可以检查PHP信息文件以了解如何配置它们。

And because they're stored on the server, you can assume they're just as secure as the rest of your server. 并且由于它们存储在服务器上,因此可以假定它们与服务器其余部分一样安全。

Yes it is more efficient. 是的,它效率更高。 Session is saved on server. 会话保存在服务器上。 However, with or without sessions you need to check if user is logged and if user has correct SESSION ID. 但是,无论有无会话,您都需要检查用户是否已登录以及用户是否具有正确的SESSION ID。 It depends on number of your columns, rows and many other things 这取决于您的列数,行数和许多其他事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM