《PHP核心技术与实践》-PHP与数据库基础
- PDO(PHP DATA OBJECT),PHP支持几乎市面上所有的数据库,但抽象度不免访问接口不够统一,所以PDO出现了,它提供了一个通用接口访问多种数据库,即抽象的数据库模型支持连接多种数据库,PDO扩展只是一个抽象层,本身不能实现数据库操作,必须使用一个特定的数据库PDO驱动访问数据库,从语法上PDO更接近MySQLi,之前学过PDO就不在这贴笔记了,只写一引起之前没学过的。
- PDO效率:PDO的CRUD效率比MYSQL直连大概低5%-15%,并且方差大于MYSQL直连,如果项目对运行效率要求严格则应MYSQL或MYSQLI,PDO负载方面,PDO开启长连接后负载高于MYSQL且比较稳定,PDO连接MYSQL/ORACLE速度比直连有优势。
- 数据库应用优化基本语句优化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.不要做无谓的排序操作而尽可能在索引中完成排序
- 索引与性能分析分析执行效率:set @@profiling=1;select * from tableName where condition;show profiles;或者show profile for query n;得到某次查询的详细性能报告以定位性能瓶颈,同一条语句的第二次查询明显比第一次查询要快是因为SQL缓存的结果。MYSQL索引建立和使用基本原则:合理设计和使用索引在关键字段的索引上,建与不建索引查询速度相差近100倍差的索引和没有索引效果一样维护索引需要成本,不是越多越好每个表索引应在5个以下,就合理利用部分索引和联合索引不在结果集中的结果单一的列上建索引,比如性别只有0和1两种在这个字段上建索引并不会有太多帮助建索引的字段结果集最好分布均匀,或者符合正太分布
- 服务器和配置的优化MYSQL常用的引擎对比-------------MyISAM-------Memory---------------InnoDB用途----------快读--------内存数据------------完整的事务支持锁------------全表锁定----全表锁定------------多种隔离级别的行锁持久性--------基于表恢复---无磁盘IO无可持久性---基于日志的恢复事务支持------不支持-------不支持--------------支持索引类型---B-tree/FullText/R-tree---Hash/B-tree---Hash/B-tree

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

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,

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.

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 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...

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�...

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

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