Jump to content

Website Optimization


Lamez
 Share

Recommended Posts

I am trying to optimize my website for speed as much as possible. However it is heavily database driven. Are there any ways to speed up each page request?

 

Also I am closing each MySql connection after every page load. Here is my database class, is that a good idea?

<?php
//For changes, see: http://www.php.net/manual/en/mysqli.connect.php
class Database{
	var $mysqli, $result, $q, $affectedRows;
	function __construct($host, $user, $pass, $db){
		$this->connect($host, $user, $pass, $db);
	}
	function connect($host, $user, $pass, $db){
		$this->mysqli = new MySQLi($host, $user, $pass, $db);
		if(mysqli_connect_error()){
			//Add Line to error handling system here...
			echo "Internal Site Error - Cannot Continue!";
			exit;
		}
	}
	function clean(){
		$str = $this->q;
		$str = @trim($str);
		if(get_magic_quotes_gpc()){
			$str = stripslashes($str);
		}
		$this->q = mysqli_real_escape_string($this->mysqli, $str);
	}
	function execute($query, $mode = MYSQLI_STORE_RESULT){
		$this->q = $query;
		$this->clean();
		$result = $this->mysqli->query($query, $mode);				
		if(is_object($result) && $result instanceof MySQLi_Result){//if result is a object and is part of the mysqli class?
			$this->result = $result;
			$this->affectedRows = $this->result->num_rows;
		}else
			$this->affectedRows = $this->mysqli->affected_rows;
		return $this;
	}
	function fetchRow(){
		return $this->result->fetch_assoc();
	}
	function fetchAll(){
		/*$row = $this->result->fetch_all($mode); 
		 See manual for the mode under mysqli_result::fetch_all
		//return !empty($row) ? $row : array();//if not empty return row, else return an array? */
		$row = array();
		while($f = $this->fetchRow()){
			$row[] = $f;
		}
		return !empty($row) ? $row : array();
	}
	function numRows(){
		return $this->affectedRows;
	}
	function delete($table, $where){
		return $this->execute("DELETE FROM ".$table." WHERE ".$where);
	}
	function deleteAll($table){
		return $this->execute("TRUNCATE ".$table);
	}
	function update($table, $set, $where){
		return $this->execute("UPDATE ".$table." SET ".$set." WHERE ".$where);
	}
	function select($table, $select = "*", $where = NULL, $cap = ""){
		if(is_null($where) || empty($where))
			return $this->execute("SELECT ".$select." FROM ".$table." ".$cap);
		else
			return $this->execute("SELECT ".$select." FROM ".$table." WHERE ".$where." ".$cap);
	}
	function lastId(){
		return $this->mysqli->insert_id;
	}
	function resetInc($table, $inc){
		$this->execute("ALTER TABLE ".$table." AUTO_INCREMENT = ".$inc);
	}
	function error(){
		return @mysqli_error($this->mysqli). " <strong><font color=\"red\">QUERY</font>: ".$this->q."</strong>";
	}
	function close(){
		@mysqli_close($this->mysqli);
	}
	function __destruct(){
		$this->close();
	}
}
$db = new Database(DB_HOST, DB_USER, DB_PASS, DB_DB);
?>

Link to comment
Share on other sites

A preprocessor like wiki uses, partition the database and use BOOLEAN mode for sting searches http://www.wizecho.com/nav=php&s=php_mysql_boolean , delay key write in mysql, pack keys if not primarily writing to it , use a compression module for smaller page size aka 40kb will be 5kb

 

And the all mighty holy grail - Create an inverted index with a stem class

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.