[英]Is where any performance difference between using text and integer & in-app constant “type” column in MySQL & php?
In my case, I have a table which stores a collection of records with similar information but each with unique type column, used in various parts of my application. 就我而言,我有一个表,该表存储具有相似信息的记录的集合,但每个记录都有唯一的类型列,这些记录用于应用程序的各个部分。 I know, I know this is "micro-optimisation" but it it an integral part of my application (it will store many records), and I would like it to be optimised, and I am also simply curious, is is faster to use text type and select it like
我知道,我知道这是“微优化”,但它是我的应用程序不可或缺的一部分(它将存储许多记录),我希望对其进行优化,而且我也很好奇,使用起来速度更快文字类型并选择它
SELECT ... WHERE type = 'some_type' or use a PHP defined constant like const SOMETYPE = 1; SELECT ... WHERE type ='some_type'或使用PHP定义的常量,例如const SOMETYPE = 1; run_query('SELECT ... WHERE type = '.SOMETYPE);
run_query('SELECT ... WHERE type ='.SOMETYPE); ?
?
String comparison will always be slower than integer comparison. 字符串比较总是比整数比较慢。 Typically, what is done is the string are stored in a separate table, perhaps called
standard_types
or whatever makes sense for the "constants" being stored. 通常,完成的操作是将字符串存储在单独的表中,该表可能称为
standard_types
或对要存储的“常量”有意义的任何东西。 That table then has a unique id field that can be referenced by other tables. 然后,该表具有唯一的ID字段,其他表可以引用该ID字段。
This way if you need the strings for reporting, the reporting queries can join to the "types" table for the display strings. 这样,如果您需要用于报告的字符串,则报告查询可以加入显示字符串的“类型”表。 Ideally, in my opinion, the id values should reflect a standard numbers that can be expressed as enum values or constants in client code;
我认为,理想情况下,id值应反映一个标准数字,该标准数字可以表示为客户端代码中的枚举值或常量。 this minimizes the dependence on the "types" table for non-reporting queries.
这样可以最大程度地减少非报告查询对“类型”表的依赖。
Some might argue against keeping the list of standard id values and their meanings coordinated across the database and one or more application codebases; 有人可能会反对在数据库和一个或多个应用程序代码库之间保持标准ID值列表及其含义的协调。 but the alternative is coordinating standard strings across all that (domains that can handle those string values quite differently).
但是另一种方法是在所有标准字符串之间协调标准字符串(可以完全不同地处理这些字符串值的域)。
The dominant time spent in any query is in fetching rows to work with. 在任何查询中花费的主要时间是在获取要使用的行中。 Functions, string vs int, etc, make only minor differences in performance.
函数,字符串与int等对性能的影响不大。 Focus on what is clean for your code, and what minimizes the number of rows touched.
专注于什么对您的代码而言是干净的,以及什么使接触的行数最少。
Once you have done that, minimize the number of round trips to the server. 完成此操作后,请尽量减少往返服务器的次数。 Even so, I have created many web pages that do 20-50 queries (each well optimized);
即使这样,我仍然创建了许多网页,这些网页可以进行20-50个查询(每个查询都进行了优化)。 the page performance is adequate.
页面性能足够。
You could also consider the ENUM
data type. 您还可以考虑
ENUM
数据类型。
sex ENUM('unk', 'male', 'female') NOT NULL
gives you WHERE sex = 'male'
, implemented as a 1-byte integer under the covers. 给您
WHERE sex = 'male'
,在幕后以1字节整数形式实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.