Here is a list of a few very simple tips for optimizing your php/mysql applications. Keep these in mind while developing.
MySQL
- MySQL is interpreted from right to left so you should put the most significant limiters as far to the right as possible.
- Only select fields you need, instead of selecting * (everything).
- Don't put things that changes very rarely in the database, instead put it in a global array in some include file.
- Use indexes on the columns in the WHERE clause and on the columns you want to ORDER BY.
- Indexes are great if you search the table alot, but it slows down insertion.
- Use the EXPLAIN command to analyze your indexes.
- If
you only want one line as a result from the database you should always
use LIMIT 1. This way mysql stops searching when it finds the first
line instead of continuing through the whole database, only to find
that there weren't any more lines that matched the query.
- If
you use $line = mysql_fetch_array($result) you'll get two ways of
accessing the columns, $line[0] and $line['columnname']. If you only
use the $line['columnname'] you should use $line =
mysql_fetch_assoc($result) instead, then there will not be any
$line[int index] array.
- Sometimes mysql_free_result() end up wasting more memory than it saves. Check the difference with memory_get_usage().
- Don't ask the database for the same stuff over and over again, save the result.
- Use NOT NULL as default value as much as you can, it speeds up execution and saves one bit.
- Use
datatypes that fits your data, not too large. For example, INT can hold
values up to 4294967295 unsigned, which is often unnecessarily big. Use
MEDIUMINT or SMALLINT where applicable.
- Make use of the default values, only insert values that differs from the default values to speed up the insertion.
PHP:
- Many code blocks might slow down the interpretation a little bit.
<?
...
...
...
?>
is faster than
<? ... ?>
<? ... ?>
<? ... ?>
- Don't concatenate when you don't need to.
"SELECT id FROM tabell WHERE id = $_SESSION[id] LIMIT 1"
is faster than:
"SELECT id FROM tabell WHERE id = ".$_SESSION['id']." LIMIT 1"
- Surrounding
your string by ' instead of " will make things interpret a little
faster since php looks for variables inside "..." but not inside '...'.
Of course you can only do this when you don't need to have variables in
the string.
- The previous item makes it all boil down to
'SELECT id FROM tabell WHERE id =
'.$_SESSION['id'].' LIMIT 1'
as the fastest way of concatenating querys.
- When echoing strings it's faster to separate them by comma instead of dot.
echo "echoing ",$variable," something";
Note: This only works with echo, which is a function that can take several strings as arguments.
-
echo
is faster than print
.
- Set the maxvalue for your for-loops before and not in the loop.
$maxvalue = 100/10;
for($i=0; $i<$maxvalue; $i++){
// Some code
}
is faster than:
for($i=0; $i<100/10; $i++){
// Some code
}
because the value is calculated once instead of ten times.
- Unset your variables to free memory, especially large arrays.
If possible it's of course always better to generate static html pages
every time something is updated or as often as an update might be
relevant instead of querying the database every time.
Further reading:
Make a comment if you have any tips that I've missed and I will add it.
分享到:
相关推荐
Investigating Bi-Level Optimization for Learning and Vision from a Unified Perspective A Survey and Beyond.zip
祁忠勇教授的这份资源《Convex Optimization for Signal Processing and Communications.pdf》详细介绍了凸优化的基本理论以及在信号处理和通信中的应用。文档强调了学习凸优化需要较为扎实的数学基础,包括但不限于...
Suvrit Sra, Sebastian Nowozin, and Stephen J. Wright, "Optimization for Machine Learning", The MIT Press Cambridge, Massachusetts London, England, 2012
Algebra, Topology, Di erential Calculus, and Optimization Theory For Computer Science and Machine Learning Jean Gallier and Jocelyn Quaintance Department of Computer and Information Science University...
本书《Beginning MySQL Database Design and Optimization: From Novice to Professional》由Jon Stephens和Chad Russell合著,旨在为初学者提供一套全面且实用的MySQL学习指南。 #### 二、数据库设计基础 1. **...
Evolutionary Computation for Modeling and Optimization - Daniel Ashlock.pdf Evolutionary Computation for Modeling and Optimization - Daniel Ashlock.pdf
PRACTICAL OPTIMIZATION Algorithms and Engineering Applications Spriinger出版。 作者: Andreas Antoniou Wu-Sheng Lu Department of Electrical and Computer Engineering University of Victoria, Canada...
Learn how to design schemas, indexes, queries and advanced MySQL features for maximum performance, and get detailed guidance for tuning your MySQL server, operating system, and hardware to their ...
MIT Optimization for Machine Learning.2012. Edited by Suvrit Sra, Sebastian Nowozin, and Stephen J. Wright
《Convex Optimization - Algorithms and Complexity》(2015 Sébastien Bubeck)即先前《Theory of Convex Optimization for Machine Learning》的升级版
《凸优化在机器学习中的应用》是一本深入探讨如何利用凸优化解决机器学习问题的专业书籍。作者Changho Suh旨在使读者理解凸优化的概念,掌握其应用,并将其有效地运用到实际的机器学习任务中。 ...
本文是塞巴斯蒂安·布贝克所著《凸优化:算法与复杂性》一书的内容概述,该书由微软研究院理论组出版。文档内容主要介绍了凸优化问题在机器学习中的应用,凸集的基本性质,以及凸优化算法的发展历程和当前的研究进展...
凸优化理论(convex optimization)在信号处理,图像处理,通信中有很广泛的应用,并受到越来越多的关注。这本书是由这个领域里的专家合写的,对理论研究和工程应用都有很大的启迪。此外,这本书中还包含了凸优化...
Written by Peter Kent, e-commerce consultant, popular speaker, and critically-acclaimed author, Search Engine Optimization For Dummies helps you build a search engine- friendly site (or fix an ...