Home Backend Development PHP Tutorial 《PHP核心技术与实践》-PHP与数据库基础

《PHP核心技术与实践》-PHP与数据库基础

Jun 23, 2016 pm 01:15 PM

  1. PDO(PHP DATA OBJECT),PHP支持几乎市面上所有的数据库,但抽象度不免访问接口不够统一,所以PDO出现了,它提供了一个通用接口访问多种数据库,即抽象的数据库模型支持连接多种数据库,PDO扩展只是一个抽象层,本身不能实现数据库操作,必须使用一个特定的数据库PDO驱动访问数据库,从语法上PDO更接近MySQLi,之前学过PDO就不在这贴笔记了,只写一引起之前没学过的。
  2. PDO效率:PDO的CRUD效率比MYSQL直连大概低5%-15%,并且方差大于MYSQL直连,如果项目对运行效率要求严格则应MYSQL或MYSQLI,PDO负载方面,PDO开启长连接后负载高于MYSQL且比较稳定,PDO连接MYSQL/ORACLE速度比直连有优势。
  3. 数据库应用优化基本语句优化10个原则:1.尽量避免在列上进行运算,这样会导致索引失败,select * from t where YEAR(d) >= 2011 优化为select * from t where d >= '2011-01-01'2.使用JOIN时,应用用小结果集驱动大结果集。同时把复杂的JOIN查询拆分成多个Query,因为JOIN多个表时可能导致更多的锁定和堵塞。3.注意LIKE模糊查询的使用,避免%%4.仅列出需要查询的字段,这对速度不会有明显影响,主要考虑内在节省。5.使用指插入语句节省交互6.limit的基数比较大时使用between,between限定比limit快,所以在海量数据访问时建议用between或是where替换掉limit,但是between的缺陷是如果中间有断行或部分不读取的情况则总读取的数量会少于预计数量!在取比较后面的数据时,通过desc方式把数据反射查找,以减少对前段数据的扫描,让limit的基数越小越好。7.不要使用rand函数获取多条随机记录8.避免使用NULL9.不要使用count(id)而应该是count(*)10.不要做无谓的排序操作而尽可能在索引中完成排序
  4. 索引与性能分析分析执行效率:set @@profiling=1;select * from tableName where condition;show profiles;或者show profile for query n;得到某次查询的详细性能报告以定位性能瓶颈,同一条语句的第二次查询明显比第一次查询要快是因为SQL缓存的结果。MYSQL索引建立和使用基本原则:合理设计和使用索引在关键字段的索引上,建与不建索引查询速度相差近100倍差的索引和没有索引效果一样维护索引需要成本,不是越多越好每个表索引应在5个以下,就合理利用部分索引和联合索引不在结果集中的结果单一的列上建索引,比如性别只有0和1两种在这个字段上建索引并不会有太多帮助建索引的字段结果集最好分布均匀,或者符合正太分布
  5. 服务器和配置的优化MYSQL常用的引擎对比-------------MyISAM-------Memory---------------InnoDB用途----------快读--------内存数据------------完整的事务支持锁------------全表锁定----全表锁定------------多种隔离级别的行锁持久性--------基于表恢复---无磁盘IO无可持久性---基于日志的恢复事务支持------不支持-------不支持--------------支持索引类型---B-tree/FullText/R-tree---Hash/B-tree---Hash/B-tree
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)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months 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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles