Profile

Cover photo
Alireza Ahmadi
Attended Shiraz University
Lives in Qom
579 followers|97,967 views
AboutPosts

Stream

Alireza Ahmadi

commented on a video on YouTube.
Shared publicly  - 
 
Dud it's really fast!! Amazing, thank you. I was considering buying new laptop but i guess there is no need for that now. 

Alireza Ahmadi

Shared publicly  - 
 
دانشگاه آزادی ها توجه پلیز! 
کسی میدونه به طور میانگین شهریه ی ارشد رشته های مهندسی چند در میاد تو دانشگاه آزاد؟ من میخوام مهندسی نرم افزار شرکت کنم. 
 ·  Translate
1
1
Alireza Ahmadi's profile photo
 
+ویتو کورلئونه share this plz! 
Add a comment...

Alireza Ahmadi

Shared publicly  - 
 
 
Success Is

“Success is doing what you want to do, when you want, where you want, with whom you want, as much as you want.” ~ Tony Robbins
4
Add a comment...

Alireza Ahmadi

Shared publicly  - 
 
If you are a web developer Join me at CakePHP community! 
1
Add a comment...

Alireza Ahmadi

Shared publicly  - 
 
That's beautiful :x 
3
Add a comment...

Alireza Ahmadi

Shared publicly  - 
 
 
Must read n share!

◄ More ►  http://yesiknowthat.com/
2
Add a comment...

Alireza Ahmadi

commented on a video on YouTube.
Shared publicly  - 
 
Hi, Great series of videos. It really help me fully understand fragments and what's going on in different scenarios. I will probably watch more videos from your channel. only one thing 
In this video you created a setCommunicator method inside Fragment and called it from Activity that i didn't liked very much. I think the better approach is to initialize Communicator on Fragment onAttached callback. this way we reduce one public method from Fragment so we get a better encapsulation for our fragment.  
That's only my idea. let me know if i'm wrong.
thank you

Alireza Ahmadi

Shared publicly  - 
 
 
5 Method to increase query performance in CakePHP. 

As you know working with databases is so easy in CakePHP. In CakePHP you can easily retrieve your information from database without having to writing sql code. 
CakePHP default configuration is perfectly good for small projects but as the projects gets bigger you may need to use some methods to increases  you query performance otherwise your server may go down due to massive amount of requests. 

In this article you'll learn 5 simple methods so you can use them to optimize your query and decrease the pressure on your server.

I'll use a sample project in this article, We have a blog system which has some posts. Each post belongs to one category and also each post has some comments and some external links. In this project we have four tables  [posts, categories, comments, links].


Let's see what actually happens when u use the default settings of cakephp. 
$posts = $this->Post->find('all');
Print_r($posts);
I used php print_r() function to print the result. The result looks something like this:
Array
(
    [0] => Array
        (
            [Post] => Array
                (
                    [id] =>1
                    [title] => How to increase query performance in cakephp 
                    [text] => some text
                )

            [Category] => Array
                (
                    [id] => 1
                    [field1] => value1
                    [field2] => value2
                    [field3] => value3
                )

            [Comment] => Array
                [0] => Array
(
                   [id] => 1
                   [field1] => value1
                   [field2] => value2
                   [field3] => value3
               )
                [1] => Array
(
                   [id] => 2
                   [field1] => value1
                   [field2] => value2
                   [field3] => value3
               )
            [ExternalLink] => Array
                [0] => Array
(
                   [id] => 1
                   [field1] => value1
                   [field2] => value2
                   [field3] => value3
               )
                [1] => Array
(
                   [id] => 2
                   [field1] => value1
                   [field2] => value2
                   [field3] => value3
               )
        )
)

As u see we have post information alongside with its all related models including category, comments and externalLinks. We may not need all this information. So I will describe some ways to cutting down information we  don’t need and by doing that we dramatically increasing our query performance. 
 

Tip Number 1- Give the right value to recursive variable
Recursive variable in the model determines how deep we get information from models and their related models. 
Recursive can have value from -1 to 2
-1 means we only get information about the model. In our example we only get information about post model. 0 means we get information from current model and the models which our currents model is belongs to. 
In our example we get information about Post model and category model. 
Recursive equal to 1 means we get information about current model and all its related models in one depth. 
Back to our example we get information about post, category, comments and externalLinks. 
In rare conditions you may want to use recursive = 2 which brings us the information in 2 level depth. Of course using this will cause serious performance problems. 

Tip Number 2- Use Containable Behavior 
By using containableBehavior u can specify that which model would returned and which one not. I recommend you to use this method instead of the "recursive" method because u have more control over the information retrieval  from DB. For example if we want to get posts and its related comments , we can set the  '1' to "recursive" variable. but in this way we have posts and all the relative model including category and external links which are not needed. To solve this problem we use containable behavior so we only get information about post and comments. 
To add containable behavior add this 2 lines of code to AppModel

var $recursive = -1;
public $actsAs = array('Containable');


The value of recursive is 1 by default. First line of code changes the default for all models so when we are not specifying which related models to bring it only brings the current model. 

When you added containable behavior you can use it by assigning a list of models as an array to 'contain' key in model find method. 
If we run code below, we get posts and their related comments. 
$this->Post->find('all', array('contain' => array('Comment')));

This is really awesome, isn't it? In my opinion you should always use Containable Behavior. 


Tip Number 3- Paginate the large data
Showing a huge amount of data, not only causes the decrease of speed and performance but also is not appropriate to use. If u have a blog with 1000 posts, u definitely don’t want to show all these posts in one page and you would rather prefer to show 10 posts on a page.
You probably know some ways to paginate data but please don't use them!! CakePHP bring us Pagination Component that helps you to paginate your records easy and fast, So instead of inventing the wheel stick with the one CakePHP already has. 
Pagination component is installed by default on your CakePHP application and u don’t need to configure anything to use it.
To paginate your data use this code in your controller 
$this->set('posts',$this->paginate('Post',$conditions)); 
As you see you can pass conditions as second parameter and its exactly like the way you use conditions in find method. 
You need to use PaginatorHelper to render your paginated data in view layer. 
First lets create headers of our table. 
<th><?php echo $this->Paginator->sort('id','Number'); ?></th>
<th><?php echo $this->Paginator->sort('title','Title'); ?></th>
<th><?php echo "text" ?></th>

First parameter is the field name in db and the 2nd parameter is the title which will echo on the screen. if u leave 2nd parameter empty CakePHP will echo the field name from DB.
You should notice that in each sorts a new query executes on DB So give the sort option only to field that really require it. You can simply use echo "something" for the field you don't want to sort base on it. Like I did for 3rd parameter. 

To show the records there is no need to do extra jobs. The process is the same as when you do not use pagination. 
<?php foreach ($posts as $post): ?>
<td><?php echo h($Post['Post']['id']); ?>&nbsp;</td>
<td><?php echo h($Post['Post']['title']); ?>&nbsp;</td>
<td><?php echo h($Post['Post']['text']); ?>&nbsp;</td>
<?php endforeach; ?>

Last thing to do  is echoing the information about paginator such as number of records, page number, next and prev buttons, etc. I don’t mention the details. u can go to http://goo.gl/JFlOM to learn more about paginator helper. 

If it seems not so easy, I have  a good news for you! If you create your project with cake bake, it uses pagination by default and it will write all the code needed for you! 
If you don't know what cake bake is, you should definitely check this http://goo.gl/O1Fp5 

Tip Number 4- Limit the fields in each query
I don't get why people use $this->Model->find('all') all the time. Why we are bringing data we don't need? 
More data from database === slower query run. 
Please, for Angelina Jolie's sake, only get the fields you need. Just use this simple code. 
$this->Model->find('all',array('fields' => array('username','password')));
Remember this, you should always include foreign  keys in field list, because if you don't cake can't make relationship between models correctly. 

Before I get to 5 and final tip I want to give you an example that uses Containable Behavior, Pagination and field limitation all together. 

class ChecksController extends AppController {

Public $paginate = array(
'contain' => array('category','comment'),
'limit' => 50, //number of row in one page
'fields => array('Post.*','Comment.id', 'Category.id','Category.title'
)));



This array configures pagination for the whole controller, so use it somewhere outside your methods! Like I did in the code. 

Tip Number 5- Let DBMS do the job instead of doing it yourself
What I mean by that? Let's say you don't know CakePHP has this awesome condition parameter and you want to get the list of managers In users table. So instead of 
$this->User->find('all', array('conditions' => array('user.permission == 'manager')));
You do this
$users = $this->User->find('all'); 
and you loop through array to find managers. 
It's stupid, right? I didn't see anybody to do that! So why I bring it up?! Because we don't do it in simple example like this, We do it in more advanced examples. But nothing changes, its equally stupid!!
SQL is a powerful language and CakePHP condition parameter can do pretty much everything sql does and even if it can't (I'm not sure about this) you still can query sql in CakePHP without any problem.
So I strongly recommend you to read more about condition parameter in CakePHP. Go here  http://goo.gl/Z9VYc
Caring about this is important for 2 main reasons
1- We are developers but the people who created DBMSes (database management systems) are brainy computer scientists! Believe it or not they do the work better than we do! 
2- In order to do the work yourself you need to get more information from database most of the time, and as I mentioned before more information from database === less query efficiency

I spent half a day writing this article and I hope this tips help you to write better applications. There is a lot more into query performance, I just tried to describe most important ones. 
I also want to thanks my dear friend +Yoosef Alidoost for helping me writing this article. 
Give your comment and let me know what you think. Thank you
1
Add a comment...

Alireza Ahmadi

Shared publicly  - 
 
Programming
Programmers of the world, Unite!
View community
1
Add a comment...

Alireza Ahmadi

Shared publicly  - 
 
دوستان این انجمن خوبیه. اگر خواستید عضو شید 
 ·  Translate
معرفی کتاب
دوستان خود را دعوت کنید
View community
1
Add a comment...

Alireza Ahmadi

Shared publicly  - 
2
Add a comment...

Alireza Ahmadi

Shared publicly  - 
 
 
Are You Creative?
1
Add a comment...
Story
Tagline
Software Architect
Introduction
I'm a software architect. I'm interested in almost all kind of programming. I work on ideas that related to e-learning and that is my primary focus. 
I like to build new things and also taking part in open source projects.

Education
  • Shiraz University
    computer software, 2010 - 2014
Basic Information
Gender
Male
Looking for
Friends, Dating, A relationship, Networking
Relationship
Single
Other names
علیرضا احمدی ‎, علی رضا احمدی
Links
Work
Occupation
Software Architecture, Programming, Teaching
Places
Map of the places this user has livedMap of the places this user has livedMap of the places this user has lived
Currently
Qom
Previously
qom
Contact Information
Home
Email
Skype
alirezaahmadi69
Yahoo
alireza_lovely_69