php - MYSQL和PHP获取记录的性能

标签 php mysql

场景 1

我有一张 table ,可以说“成员(member)”。在该表“member”中,我有 7 个字段(memid、login_name、password、age、city、phone、country)。在我的表中,我有 10K 条记录。我需要获取一条记录。所以我使用这样的查询

mysql_query("select * from member where memid=999");

场景 2

我有同一张名为“member”的表,但我像这个member和member_txt一样拆分表。所以在我的member_txt表中我有memid,age,phone,city,country)并且在我的member表中我有memid ,登录名,密码。

哪种场景是快速获取数据的最佳方案?要么使用单个表,要么将表分成两个引用?

注意:我需要在 PHP 和 MYSQL 中获取特定数据。请让我知道哪种方法是最好的。 我们有 10K 条记录

最佳答案

为了您自己的健康,请使用单表方法。

只要您使用 memid 主键,事情就会快如闪电。这是因为 PRIMARY KEY 自动分配一个索引,该索引基本上可以告诉数据的确切位置,并且无需像其他方式那样检查数据。

来自http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html

Indexes are used to find rows with specific column values quickly. Without an index, MySQL must begin with the first row and then read through the entire table to find the relevant rows. The larger the table, the more this costs. If the table has an index for the columns in question, MySQL can quickly determine the position to seek to in the middle of the data file without having to look at all the data. If a table has 1,000 rows, this is at least 100 times faster than reading sequentially. If you need to access most of the rows, it is faster to read sequentially, because this minimizes disk seeks.

第二种方法只会使您的系统变得更加复杂,并且没有任何好处。

使用场景 1。

关于php - MYSQL和PHP获取记录的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10749119/

相关文章:

php - PHP curl 方法可以在同一个请求中上传一个文件和一个带有前导@字符的字符串吗?

php - 如何在通知图标的 html/php 标题栏中将数据显示为来自数据库的通知

MySQL 错误 : specified key was too long

mysql - 如何在 SQL 中将选定的日期列格式化为相对时间格式

php - 从数据库中存储的变量未在 HTML 页面上回显

php - 检查电子邮件是否已存在以及同一函数中的 FILTER_VALIDATE_EMAIL

php - 在 .htaccess 中动态解析 URL

php - Laravel 嵌套查询生成器 Json 响应

php - 根据用户答案从数据库中选择宠元素种/汽车/自行车/等的算法

php - MySQL 数据库搜索字符串 - 部分单词