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

  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
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
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool