带数据库存储选项的 Codeigniter 会话类未优化?

发布于 2024-11-27 12:31:13 字数 1010 浏览 0 评论 0 原文

我使用 codeigniter 的会话类,并可以选择将会话数据存储在数据库中。这是为每个用户请求检索会话而运行的选择查询的示例:

SELECT *
FROM (`ci_sessions`)
WHERE `session_id` = 'f7fd61f08a229kdu3093130a3da17e14'
AND `user_agent` = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:5.'

以下是 用户指南

CREATE TABLE IF NOT EXISTS  `ci_sessions` (
session_id varchar(40) DEFAULT '0' NOT NULL,
ip_address varchar(16) DEFAULT '0' NOT NULL,
user_agent varchar(50) NOT NULL,
last_activity int(10) unsigned DEFAULT 0 NOT NULL,
user_data text DEFAULT '' NOT NULL,
PRIMARY KEY (session_id)
);

据我了解,每当您有一个打算返回单个结果的查询时,最好使用 LIMIT 0, 1,以便数据库引擎找到所需的行它只是返回而不是继续扫描整个表以查找更多匹配项。因此,将此查询编写为:

SELECT *
FROM (`ci_sessions`)
WHERE `session_id` = 'f7fd61f08a229kdu3093130a3da17e14'
AND `user_agent` = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:5.'
LIMIT 0, 1

是否有任何原因没有这样编写?

I use codeigniter's session class with the option to store session data in a database. This is an example of the select query that runs for every user request to retrieve a session:

SELECT *
FROM (`ci_sessions`)
WHERE `session_id` = 'f7fd61f08a229kdu3093130a3da17e14'
AND `user_agent` = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:5.'

Here is the table structure for session data as defined in the user guide:

CREATE TABLE IF NOT EXISTS  `ci_sessions` (
session_id varchar(40) DEFAULT '0' NOT NULL,
ip_address varchar(16) DEFAULT '0' NOT NULL,
user_agent varchar(50) NOT NULL,
last_activity int(10) unsigned DEFAULT 0 NOT NULL,
user_data text DEFAULT '' NOT NULL,
PRIMARY KEY (session_id)
);

It's my understanding that whenever you have a query that's intended to return a single result it's good practice to use LIMIT 0, 1 so that when the database engine finds the desired row it simply returns rather than continuing to scan the entire table for more matches. Therefore would it be more efficient for this query to be written as:

SELECT *
FROM (`ci_sessions`)
WHERE `session_id` = 'f7fd61f08a229kdu3093130a3da17e14'
AND `user_agent` = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:5.'
LIMIT 0, 1

Is there any reason it isn't already written this way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

GRAY°灰色天空 2024-12-04 12:31:13

可能只有一行与 user_agent 和 session_id 匹配,因此不需要限制选择的数量,它已经被限制为唯一的。

该问题已报告给 Bitbucket 上的 Codeigniter Reactor 开发人员,并被视为无效而被驳回:

https://bitbucket.org/ellislab/codeigniter-reactor/issue/422/session-class-should-use-limit-1-when

他们的回应:

session_id 字段是主键,因此它将是唯一的行

是在查询索引/唯一字段时使用 MySQL“LIMIT 1”有什么意义?

所以看起来这实际上并不是一种优化,它只是不必要的。

There could be only one row, matching the user_agent and the session_id, so there will be no need to limit the number of selections, it is already limited by being the only one.

The issue has been reported to the Codeigniter Reactor devs on Bitbucket and dismissed as invalid:

https://bitbucket.org/ellislab/codeigniter-reactor/issue/422/session-class-should-use-limit-1-when

Their response:

the session_id field is a primary key so it's going to be a unique row

Is there any point using MySQL "LIMIT 1" when querying on indexed/unique field?

So it would appear that this is not in fact an optimization, it's just unnecessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文