Thursday, 10 October 2019

How to speed up your application with MongoDb?

How to speed up MongoDb?

1) Make Sure you application have Sufficient RAM
If your application don't have enough RAM, then please increase it.


2) Analyze Your Queries
Analyse the queries with explain like below:
db.user.find('{"likes": {$gt:10}"likes"}').explain("executionStats");
You can check the problem with the queries and fix the issue.
Also please fetch only those records which are using. Don't fetch un-necessary field and records from database.


3) Limit the Records
If you are using 10 records but getting 100 records. Means you are getting 99 record extra that is wrong. So please limit the record with limit command.
db.user.find('{"likes": {$gt:10}"likes"}').limit(10);



4) Add Appropriate Indexes
You can use the index wherever are required (most used in search query). You can add indexes on Single/Multiple field. In this way you can speed the application by speedup the queries.
db.user.createIndex({ country: 1 });



5) Sorting
Un-necessary sorting can also have problem, So sort the records wherever required. Avoid un-necessary sorting. Example of Sorting (-1 no sorting, 1 ascending , 0 descending )
Sort by Country and then City.
db.user.find().sort({ country: 1, city: 1 });

Sort by Country only (Even we have added city in our indexes).
db.user.find().sort({ country: 1, city: -1 });

Sort by city only (Even we have added country in our indexes).
db.user.find().sort({ country: -1, city: 1 });



6) Check Your MongoDB Log
As we know, logging the queries take some time to logged. It make take about 100Mili seconds for each log query.
So, You should log the queries carefully because un-necessary loging data may slow the system.
db.setLogLevel(0);
0 is the MongoDB's default log verbosity level, to include Informational messages.
1 to 5 increases the verbosity level to include Debug messages.


7) Understand the Query Profiling
You can get Profiling Level (current).
db.getProfilingLevel()

You can set Profiling Level.
db.setProfilingLevel(1)

Following the 3 Profiling Level.
  1. -1 for inherit the profiling from parent.
  2. 0 for no profiling.
  3. 1 for slow operations.
  4. 2 for all operations.

8) Multiple Database Connection
If you are running heavy site and few queries are taking taking more time due to which queries are completing in asynchronous.
Means query start time are q1, q2, q3 but completing time are q2,q1,q3.
To fix this problem.
For this, you can create multiple Database connection and handle like below:
  1. One to handle the majority of fast queries.
  2. One to handle slower document inserts and updates.
  3. One to handle complex report generation.



9) Set Maximum Execution time

MongoDB commands run as long as they need. A slowly-executing query can hold up others, and your web application may time out.
You can set the max execution time like below:
db.user.find('{"likes": {$gt:10}"likes"}').maxTimeMS(100);



10) Rebuild Your Indexes

You need re-index, if your collection fall in following:
  1. Collection size has increased significantly.
  2. Indexes are consuming a disproportionate amount of disk space.



Saturday, 5 October 2019

FuelPHP Interview Questions and Answers for Beginners

FuelPHP Interview Questions and Answers for Beginners

Question: What is FuelPHP?
FuelPHP is PHP Framework written in PHP, based on the HMVC pattern.


Question: Is FuelPHP Open Source?
Yes, It is Open Source.


Question: What is minimum PHP Version required for FulePHP?
PHP 5.4+


Question: Is FulePHP support Multilingual?
Yes, It support Multilingual.


Question: What is offical website of FulePHP?
fuelphp.com


Question: What are Key Features of FuelPHP?
  1. URL routing system
  2. RESTful implementation
  3. HMVC implementation
  4. Form Data validation
  5. ORM (Object Relational Mapper)
  6. Vulnerability protections like XSS, CSRF, SQL Protection and encode output.
  7. Caching System



Question: What is full form of HMVC?
Hierarchical-Model-View-Controller


Question: What is HMVC?
HMVC is an evolution of the MVC pattern.


Question: What are benefits of HMVC?
  1. Modularization
  2. Organization
  3. Reusability
  4. Extendibility



Question: How to get Query in FulePHP?
$userQueryToExecute = Model_Article::query()
        ->select('users')        
        ->where('blocked', '=', 1);

echo $userQueryToExecute->get_query();



Question: How to check that Redis server is running?
try
{
    $redis = \Redis::instance();    
}
catch(\RedisException $e)
{
    //here error will come
}



Question: How to use join with condition?
$queryObj = \Services\Model_Org::query()
->related('org')
->related('profile_image')->related( array(  'comments' => array(   'where' => array(    array('visible' , '=' , '0')   )
  ) ))
->where('rating','!=', 'null')
->order_by('rating','desc')
->get();