开发者

Does including require("db.php") at the top of every php page slow things down?

My db.php file connects to, selects, and creates databases and tables if they do not exist. When I require("db.php") at the top of every page does it re-run that code every time? Would this eventually cause a slowdown, albeit a slight one? Should I really be connecting to a database, selecting it, and checking to crea开发者_开发技巧te new tables if they do not already exist on every single page that requires querying the database? Is there a best practice for this type of situation? Am I worrying over nothing?


The "Create databases and tables if they do not exist" part I would remove from the bootstrap process: It might be costly, and is definitely unnecessary to run on every request. Have your code crash gracefully if a table doesn't exist instead.

Just establishing a database connection is however pretty standard for most PHP applications, as it is usually needed in most, if not all, contexts.

There is the possibility of building a database wrapper that "Lazy connects", i.e. establishes the connection only when it is actually needed, but I wouldn't worry about this unless you really have reason to based on performance measurements.


I would recommend you,:

  1. not to establish connection to Db-server until you are actually querying it; just put it in standby mode and connect when first Db query comes in. This is how Zend_Db of Zend Framework works, and most frameworks act with the same thought: Have resources ready, and use them when necessary ONLY.

  2. to keep your code and project-framework as tidier as possible. Slow code is always preferable to the Unreadable one.

  3. not to mix work-routines with diagnostics. Checking for Db's and its tables' integrity is a task for diagnostics, and is usually performed by Sysadmin in regular basis (weekly, monthly etc). Work-routines on the other hand, always assume things are honkie dorie and hope for the best outcome with the contingency precautions set in place (which are built for the worst case scenarios, and save the day), in case things go south.


Everytime you do a require, the file is added to the script code. This code needs to be processed by the php compiler at runtime. So, you're adding possible about 1kb of code that needs to be processed by your cpu everytime you run the script. So yes, you're slowing it down, but the slowdown is neglible, it would only be noticeable if you start adding files over 1MB. 1MB of code is a lot of code.


You can just put the connection and selection in that file, but creating them if they don't exist seems useless to me. If you create them, they usually don't just get lost. Though establishing a connection is often the slowest part of the code, but you don't have much choice - however you could try using mysql_pconnect which is faster once a connection is there. Check here.
The extra time used when requiring a file is not visible to the naked eye, so to speak, so you don't have to worry about that. Using more files is just easier to manage, as long as you don't overuse it.


It's safe to say that if you don't need a piece of code, don't call it. However, a file to connect to your database won't take too much (probably, 5 o 6 lines of not very demanding code).

Anyway, if you need to connect to your database, you don't have more options than include that file.


first use require_once not just require , basically require_once("db.php") will just copy the contents of code db.php and paste it where its getting called . Hence overhead = open file + read file + copy file + paste file and lastly interpret that extra data .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜