简体   繁体   English

在MySQL和php中使用文本与整数和应用内常量“类型”列之间的性能差异在哪里?

[英]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.

相关问题 是否有一个 php 函数可以将任意 2 个整数之间的差作为正整数返回? - is there a php function to return the difference between any 2 integer numbers as a positive integer? 使用“=”和“LIKE”之间有什么性能差异吗? - Is there any performance difference between using “ = ” and “ LIKE ”? 在Laravel的'join'或'where'中使用'='有什么区别吗? - Is there any difference between using '=' in a 'join' or 'where' on Laravel? mysql中“ WHERE”和“ IN”之间的区别 - Difference between “WHERE ” and “IN” in mysql PHP / MySQL-如何使用PHP在网页上的任何位置显示PHP生成的文本? - PHP/MySQL - How to display PHP generated text any where on a web page using PHP? 使用PHP和MYSQL编写的这些准备好的语句有什么区别吗? - Is there any difference in these prepared statements using PHP and MYSQL? PHP与整数之间的差异 - PHP difference between int and integer Azure:使用MySQL in-app(预览版)的Wordpress数据库的默认用户/通行证在哪里? - Azure: Where is default user/pass for Wordpress database using MySQL in-app(preview)? PHP中(int)1和1之间有什么区别? - Any Difference Between (int) 1 and 1 in PHP? 如何使用TEXT类型字段在MySQL上获得快速性能? - How to gain fast performance on MySQL using TEXT type field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM