Home php教程 php手册 再谈PHP开发者常犯的10个MySQL错误

再谈PHP开发者常犯的10个MySQL错误

Jun 13, 2016 am 10:46 AM
mysql php Developer article of mistake

最近看到一篇文章:《PHP开发者常犯的10个MySQL错误》,发现文中不少内容陈旧,随着时间推移技术发展变化而变得不适用。为了防止误导新手,特本着与时俱进的精神写出此文,绝非对原文作者的不尊重。

1.使用MyISAM而不是InnoDB
完全错误,反驳理由:
首先原文说MyISAM是默认使用的,而实际上到了MySQL 5.5.x,InnoDB已经成为了默认的表引擎。

另外,简单的使用InnoDB不是解决所有问题的方法,盲目的使用甚至会使应用性能下降10%乃至40%。

最佳方法还是针对具体业务具体处理,例如论坛中版块表,新闻分类表,各种码表等长时间不操作的表,还是要用性能优异的MyISAM引擎。
而需要用到事务处理的例如用户、账目、流水等严格要求数据完整性和时序性的,则需要用InnoDB引擎,并且应用也要用好事务处理机制。当然,事务处理必然要带来大量的性能损耗,但是这在简单高并发应用上是必须的。

最后,外键约束在公共web互联网应用上一般是不用的,因为他会严重影响性能。数据完整性还是靠程序员或者应用架构本身的健壮来维护。而正规的第三范式只是在企业内部MIS系统和12306这种网站上使用。

2.使用PHP的mysql方法
不完全错,但要酌情选用:
mysqli固然好,但是不是所有的服务器都为PHP编译了mysqli的支持。
当你的应用如果是能确定只用自己部署的服务器,而应用也是完全自己开发,则mysqli是最好的选择。
但是一旦你的应用有可能部署在虚拟主机或者由其他人部署(例如分布式项目),还是老老实实使用mysql函数集吧,好好封装一下或者使用成熟框架杜绝sql注入。

3.不过滤用户输入
这一点不用说了,要么MagicQuote,要么选用成熟框架。sql注入老话题了。

4.不使用UTF-8
大部分情况下对,但也要认真考虑:
要知道,一个UTF-8字符占3个字节,所以比GBK等其他编码的文件大33%。换句话说,相同的网页用UTF-8编码如果是100KB,那么换成GBK编码则只有66KB。所以即便你的PHP确定要用UTF-8,那么前端页面也要根据情况选择需要的编码。但是,如果PHP用UTF-8,前端模版是GBK,再加上模版引擎不强大,那么转码工作够你受的。所以尽可能的选用自己需要的编码,而不是简单的选择UTF-8了事。
最后啰嗦一句:UTF-8下:strlen("我")=3,而GBK下:strlen("我")=2

5.该用SQL的地方使用PHP
同样酌情考虑:
例如,有些人习惯在建表时,默认值填写CURRENT_TIMESTAMP,用来达到注册时间、发帖时间的效果。 或者在时间判断的SQL语句中,写类似SELECT x FROM tab1 WHERE regdate 正确做法是:不要使用MySQL的任何时间函数,而是在应用里计算时间。如果是分布式应用,一定要有时间服务器来统一管理时间。
而文中说的一些MySQL数学函数 ,也是要慎用。因为在大型应用中,数据库的负担往往是最大的,而复杂的WHERE语句又是造成慢查询的元凶。所以,要把计算尽可能的放在廉价的、不影响全局稳定的应用服务器上,而不是核心数据库上。

6.不优化查询
这点也不用说了,大型应用上甚至不允许使用各种JOIN,哪怕生写两条查询,查回来在用PHP合并数据。

7.使用错误的数据类型
INT,TinyINT,VARCHAR,CHAR,TEXT这些字段类型的合理选用无可厚非。
而Date、DateTime、TIMESTAMP这三种类型,在大型应用中是绝对不可以使用的,而是要用INT(10) UNSIGNED代替。
一个是性能,另外就是应用中尤其是PHP对UNIX_TIMESTAMP时间戳的转化实在太方便了。用Date要输出各种时间格式反而麻烦。

8.在SELECT查询中使用*
共勉

9.索引不足或者过度索引
索引是必须的,但是如果索引都解决不了的查询,考虑memcache或者nosql解决方案吧。

10.不备份
这条是作者凑数么?

11.另外:不考虑其他数据库
这条相当正确。应用中不仅要针对应用选择其他数据库,甚至还要针对具体的业务类型,在同一套应用中并行使用多种数据库。哪怕不是数据库,而是其他各种缓存、内存存储等解决方案。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

RDS MySQL integration with Redshift zero ETL RDS MySQL integration with Redshift zero ETL Apr 08, 2025 pm 07:06 PM

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

How to fill in mysql username and password How to fill in mysql username and password Apr 08, 2025 pm 07:09 PM

To fill in the MySQL username and password: 1. Determine the username and password; 2. Connect to the database; 3. Use the username and password to execute queries and commands.

MySQL: The Ease of Data Management for Beginners MySQL: The Ease of Data Management for Beginners Apr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

Query optimization in MySQL is essential for improving database performance, especially when dealing with large data sets Query optimization in MySQL is essential for improving database performance, especially when dealing with large data sets Apr 08, 2025 pm 07:12 PM

1. Use the correct index to speed up data retrieval by reducing the amount of data scanned select*frommployeeswherelast_name='smith'; if you look up a column of a table multiple times, create an index for that column. If you or your app needs data from multiple columns according to the criteria, create a composite index 2. Avoid select * only those required columns, if you select all unwanted columns, this will only consume more server memory and cause the server to slow down at high load or frequency times For example, your table contains columns such as created_at and updated_at and timestamps, and then avoid selecting * because they do not require inefficient query se

How to view mysql How to view mysql Apr 08, 2025 pm 07:21 PM

View the MySQL database with the following command: Connect to the server: mysql -u Username -p Password Run SHOW DATABASES; Command to get all existing databases Select database: USE database name; View table: SHOW TABLES; View table structure: DESCRIBE table name; View data: SELECT * FROM table name;

Can I retrieve the database password in Navicat? Can I retrieve the database password in Navicat? Apr 08, 2025 pm 09:51 PM

Navicat itself does not store the database password, and can only retrieve the encrypted password. Solution: 1. Check the password manager; 2. Check Navicat's "Remember Password" function; 3. Reset the database password; 4. Contact the database administrator.

How to copy and paste mysql How to copy and paste mysql Apr 08, 2025 pm 07:18 PM

Copy and paste in MySQL includes the following steps: select the data, copy with Ctrl C (Windows) or Cmd C (Mac); right-click at the target location, select Paste or use Ctrl V (Windows) or Cmd V (Mac); the copied data is inserted into the target location, or replace existing data (depending on whether the data already exists at the target location).

See all articles