thinkphp中new PDO()需要几秒钟时间?
今天发现网站打开特别慢,然后分析原因,发现是数据库查询花费时间特别长,一个最简单的查询都至少4秒以上,然后分析最终发现原因是执行new PDO(...) 时间花费4秒以上,请问可能是因为什么原因?
网站用的thinkphp框架,服务器版本:windows serve 2008.
数据库在另外一台服务器上。
另:1.new PDO(localhost...) 不存在该问题
2.new PDO(另外一个windows上运行的数据库) 出现同样问题
3.new PDO(另外一个linux上运行的数据库) 不存在该问题
4.在本地环境中new PDO(同一数据库) 不存在该问题
...
请求大神解惑啊!!!谢谢
2016-01-28 09:32 更新:
测试代码如下:
<code>$t1=microtime(true); $dsn = "mysql:host=59.175.142.**;dbname=iqgame"; $db = new PDO($dsn, '****', '****');s $t2=microtime(true); echo $t2-$t1; // 输出4s多</code>
1.web服务器与数据库服务器均在外网,连接数据库使用IP连接
2.这个问题是突然之间出现的,以前没有这么严重的连接时间
3.感谢大家伙的热心帮助~
2016-01-28 10:11 再次更新:
感谢 @LakeChan 的提醒,我试着将web服务器中的防火墙关闭,则瞬间解决问题!!!
然后我也不知道该怎么配置防火墙才能屏蔽该问题,只能一条一条规则的尝试,然后,待我重新打开防火墙后发现问题居然没有再出现了!!!
这种心情,感觉被狗[和谐]日了一样!!!
总结,虽然不知道究竟发生了什么!但是真心觉得还是Linux大法好啊!!!
最后,真诚的感谢各位同仁的热心帮助,祝各位年终奖翻倍!!!
回复内容:
今天发现网站打开特别慢,然后分析原因,发现是数据库查询花费时间特别长,一个最简单的查询都至少4秒以上,然后分析最终发现原因是执行new PDO(...) 时间花费4秒以上,请问可能是因为什么原因?
网站用的thinkphp框架,服务器版本:windows serve 2008.
数据库在另外一台服务器上。
另:1.new PDO(localhost...) 不存在该问题
2.new PDO(另外一个windows上运行的数据库) 出现同样问题
3.new PDO(另外一个linux上运行的数据库) 不存在该问题
4.在本地环境中new PDO(同一数据库) 不存在该问题
...
请求大神解惑啊!!!谢谢
2016-01-28 09:32 更新:
测试代码如下:
<code>$t1=microtime(true); $dsn = "mysql:host=59.175.142.**;dbname=iqgame"; $db = new PDO($dsn, '****', '****');s $t2=microtime(true); echo $t2-$t1; // 输出4s多</code>
1.web服务器与数据库服务器均在外网,连接数据库使用IP连接
2.这个问题是突然之间出现的,以前没有这么严重的连接时间
3.感谢大家伙的热心帮助~
2016-01-28 10:11 再次更新:
感谢 @LakeChan 的提醒,我试着将web服务器中的防火墙关闭,则瞬间解决问题!!!
然后我也不知道该怎么配置防火墙才能屏蔽该问题,只能一条一条规则的尝试,然后,待我重新打开防火墙后发现问题居然没有再出现了!!!
这种心情,感觉被狗[和谐]日了一样!!!
总结,虽然不知道究竟发生了什么!但是真心觉得还是Linux大法好啊!!!
最后,真诚的感谢各位同仁的热心帮助,祝各位年终奖翻倍!!!
1.使用IP而不是域名,使用域名会让PDO在连接之前进行一次不必要的dns lookup,当DNS缓存过于巨大的时候,这个问题可能会更严重。(即使是运行在本机,使用 127.0.0.1 而不是 localhost)
2.如果连接到内网的其他服务器,内网的网络节点是否工作正常?是否会有防火墙处理连接过慢的问题?
web 服务器连数据库服务器是局域网还是公网?
看不到代码,难说。与数据库是IP连的还是域名连的也有关系
首先看看是不是因为IPV6的原因导致连接慢,其次看看Mysql配置是否合理。PHP不会出现连接慢的问题,问题肯定在mysql上
可能是mysql反向解析的问题,可以对比一下windows和linux服务器的myslq的配置文件,看下是否有:
skip-name-resolve
另外,mysql日志应该也会有相应的记录。
楼主,php的pdo是C写的扩展,效率是极高的,基本一个连接是毫秒级,所以不用怀疑扩展本身。
1、先看看是否代码问题,单独写个demo用pdo连接下看,耗时多少
2、telnet一下web机跟sql机的耗时看看,是否网络问题(最常见)

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

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

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











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
