Monday, 3 February 2020

Delete big data using MySQL Partitioning

What is MySQL Partitioning?

Partitioning is a way in which a database (MySQL in this case) splits its actual data down into separate tables, but still get treated as a single table by the SQL layer.

Partitions are also very useful in dealing with data rotation. If MySQL can easily identify rows to delete and map them to single partition, instead of running DELETE FROM table WHERE

There are 2 types of partitioning:

  1. Vertical Partitioning
  2. Horizontal Partitioning

Horizontal partitioning means that all rows matching the partitioning function will be assigned to different physical partitions. Vertical partitioning allows different table columns to be split into different physical partitions. Currently, MySQL supports horizontal partitioning, but not vertical.


Delete Millions of Records using MySql Table Partitioning Technique


Suppose we have table that have millions of records so we can ALTER the table and make table partitioning according to our needs.



Before Run any Action Query on Production Server:
1. Take backup of the table/database
2. Recommend to do this activity on low traffic (during night)
3. Perform this action on test server first before go to live server




ALTER TABLE `table_name` PARTITION BY RANGE (UNIX_TIMESTAMP(table_name_timestamp)) 
(PARTITION pYear2017 VALUES LESS THAN (UNIX_TIMESTAMP('2018-01-01 00:00:00')), 
PARTITION pYear2018 VALUES LESS THAN (UNIX_TIMESTAMP('2019-01-01 00:00:00')), 
PARTITION pYear2019 VALUES LESS THAN (MAXVALUE));


After running the above query we’ll have 3 partitions (pYear2017, pYear2018, pYear2019) of table as showing in below image.



As we can see there are many options for the Partitions. We can Analyze, Check, Optimize, Rebuild, Repair, Truncate and Drop the Partition.

We can see all the records of the selected partition using below query. I select pYear2018 partition.
This query will show all the records of year 2018



SELECT * FROM `table_name` PARTITION(pYear2018);



Now if we want to drop any partition This will also DELETE the data related to the selected partition(s). And if we truncate the partition records will delete but partition remain.


We can select any action (eg: Drop) and click on go to perform the action. Or we can run sql query by selection any partition eg: pYear2018




ALTER TABLE `table_name` DROP PARTITION pYear2018;



The above query will drop partition and delete all the records of year 2018 very fast.


We can use same technique to delete other records with other conditions as well.
This technique is very useful for the big data tables.





Tuesday, 31 December 2019

Check for Palindrome string using PHP


The characters read the same backward as forward. Some examples of palindromic words are redivider, deified, civic, radar, level, rotor, kayak, reviver, racecar, redder, madam, and refer.




function palindrome($string)
{
    return strrev($string) == $string ? true : false;
} 



Results





echo palindrome('SOOS') == true ? 'This is Palindrome' : 'This is not Palindrome';

Output: This is Palindrome
Explanation: Reversing SOOS will also get SOOS





echo palindrome('SOOSS') == true ? 'This is Palindrome' : 'This is not Palindrome';

Output: This is not Palindrome
Explanation: Reversing SOOSS will get SSOOS




Thursday, 3 December 2015

Basic SHELL commands (for Putty)

Overview of the most common basic SHELL commands. I typically use PuTTY, a free telnet and SSH Client for Windows and Unix platforms. It is lightweight and easy to use and doesn’t need installing (can run from a flash disk).


Navigating directories

cd
…change directory, method used for moving from one folder to another.
cd foldername/foldername2
…would take you to foldername2.
cd ..
…moves up one directory.
cd /
…moves you to root.
pwd
…prints to console your present working directory – where you currently are.

List contents

ls
…list contents of current directory.
ls -l
…shows long format inc. group owner, size, date modified, permissions.
ls -a
…shows ALL files inc. hidden files.
ls -R
…lists contents of sub directories recursively.
ls -la
…the above options can be used at the same time.
ls foldername/foldername
…list items in folder without actually moving to it.

Creating files and directories/folders

touch file.html
…use the touch command to create files.
rm file.html
…use the rm command to remove a file.
mkdir myfolder
…use the mkdir command to create a new directory/folder.
rmdir myfolder
…use the rmdir command to remove a directory/folder, folder must be empty.
mv folder1/file.html folder2/file.html
…use the mv command to move a file. Can also be used to rename a file.

Compressing and backing up files and folders

zip -r foo.zip foo/
…compress the folder ‘foo’ and all of its contents into a zip file called ‘foo.zip’.
zip foo.zip foo.html
…compress the file’foo.html’ into a zip file called ‘foo.zip’.

File and directory/folder permissions and ownership

chmod 755 file.html
…changes the file.html file permissions, same for a folder.
chmod -r 755 myfolder
…changes permissions for that folder and all folders and files inside it recursively.
Here are the chmod octal numeric values
700: only owner can read
755: everyone can read but not write
775: only group can read and write
770: no-one but group can read
666: everyone can read and write
1777: everyone can read,write,execute

chown user:myself file.html
…changes the ownership of file.html to the user called ‘myself’.

 

Tuesday, 8 September 2015

MongoDB Installation + CRUD Guide

I am sharing the simplest method for MongoDB installation + db creation

Install mongodb from below link as per your OS


Go to your installed MongoDB directory/bin and create a folder with name "data"

Open cmd, go to your installed MongoDB directory/bin with the help of "cd"

execute following command:

> mongod.exe --dbpath "d:\mongo\data"  // as i have installed MongoDB in D:\mongo\ 


Wait for establishing connection

open another cmd, again go to your installed MongoDB directory/bin with the help of "cd" 

execute following command:

> mongo.exe 

Now you have access to CRUD your own db
Below is the sample code to create collection with document


> use test // it creates db with name "test"

> db.createCollection(collection_name) // it creates a Collection

> db.collection_name.insert({"name":"testing entry"})   // it creates document like "{"name":"testing entry"}"

> db.collection_name.find() // it will show all documents related to collection "collection_name"

> db.collection_name.drop() // it will delete the collection

> show dbs // it will show all databases

Note:
Mongo Db is based on no-sql. so it is bit faster 
May be there are some more professional/easy ways to do that.

Plz keep this information handy
and stay tuned, will keep sharing short articles about MeanSTACK!

Monday, 7 September 2015

Express.js Running Application

There is a tutorial for installing new running Express js application!

Installing Node.js:
1- Go to this link "https://nodejs.org/" and install node.js.
2- Go to Shell and intall express generator using "npm install express-generator -g"

For New Express Application:
1- Make a directory i.e., D:\nodePractice.
2- Go to this directory and from shell "npm install express". It will create a folder in this directory called 

"node-modules".
3- Go to "node-modules" and from shell make "package.json" file using "npm init"!
While creating this file it is gonna ask for some things like appname, description, test-command etc. Enter the desired text there!
3- Go to "node-modules" directory and from shell install express application using "express" command.
4- Add This code to your app.js file created in "node-modules" directory:

var server = app.listen(3000, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});

5- Run app.js file!
6- intall missing modules using 'npm intall <module-name>' e.g, "npm install serve-favicon"
7- Run app.js again! Bang there you are...

Wednesday, 10 September 2014

WordPress and Magento. To integrate or not


Blogging is the big part of company’s marketing activities and blog extensions are widely needed as a part of web-site. Therefore these two very powerful platforms are often integrated together. Magneto is used for  managing products, inventory, sales, payment gateways and pages, while WordPress is for blog posts.

Why WordPress?
  • First of all, there are two main reasons to add a blog to your web-site. The first is community interaction. The second is SEO content generation. And for both purposes, WordPress is the fully functional solution.
The rich SEO features built into WordPress like:
  • pingbacks, which notify your website when someone posts an article about it
  • trackbacks, which notifies a website when you post an article referencing it
These features are free at WordPress and create a rich network of backlinks. None of any Magento extensions provide these features.
  • The Magento WordPress Integration is invisible to your end user
You have the ability to customise the fields you need . WordPress  skin is identical to your Magento theme and vice versa, you can leave your CSS files and graphics in the Magento instance.
  • Flexible WordPress Plugins
There are thousands of  WordPress plugins that can improve and add the huge amount of  features and functionality to your WordPress blog.
Learn IT Delight’s successful stories based on Magento and WordPress integration:

Bringing it all together, Magento and WordPress integration  gives the user the full flexibility of WordPress and the powerful product inventory system of Magento. Building an e-Commerce store, you can use Magento as a backend and WordPress as a frontend.
WordPress blog integrated on Mangento website is the great way to promote your online store and bring more customers to your online business.

Tuesday, 9 September 2014

MVC vs MVVM vs MVP

MVC vs MVVM vs MVP. What a controversial topic that many developers can spend hours and hours debating and arguing about.

For several years +AngularJS was closer to MVC (or rather one of its client-side variants), but over time and thanks to many refactorings and api improvements, it's now closer to MVVM – the $scope object could be considered the ViewModel that is being decorated by a function that we call a Controller.

Being able to categorize a framework and put it into one of the MV* buckets has some advantages. It can help developers get more comfortable with its apis by making it easier to create a mental model that represents the application that is being built with the framework. It can also help to establish terminology that is used by developers.

Having said, I'd rather see developers build kick-ass apps that are well-designed and follow separation of concerns, than see them waste time arguing about MV* nonsense. And for this reason, I hereby declare AngularJS to be MVW framework - Model-View-Whatever. Where Whatever stands for "whatever works for you".



Angular gives you a lot of flexibility to nicely separate presentation logic from business logic and presentation state. Please use it fuel your productivity and application maintainability rather than heated discussions about things that at the end of the day don't matter that much.

Friday, 8 August 2014

Print and Preview specific Div with Javascript

Spouse we have a DIV that we want to print and its ID is 'printPDF'

 <div id="printPDF"> This area will be print</div>
var style_sheet = 'baseUrl . "/css/style.css"; ?>'; //Css file,Base url
var style_sheet = '';// Style sheet that you want to include


 var printContent = document.getElementById('printPDF').innerHTML;
        var printContent2 = "" + style_sheet + "" + printContent + "";
        var windowUrl = 'about:blank';
        var uniqueName = new Date();
        var windowName = 'Print' + uniqueName.getTime();
        var printWindow = window.open(windowUrl, windowName, 'left=50000,top=50000,width=0,height=0');
        printWindow.document.write(printContent2);
        printWindow.document.close();
        printWindow.focus();
        printWindow.print();
        printWindow.close();

Wednesday, 6 August 2014

Playing With Date in PHP

PHP program that prints every date from your birthday to three months after. On the 1st of each month, write the month name. Write “Free lunch” next to fridays and on dates where the day number is multiple of five print “Casual” next to the day {Day Name}.
Sample Section of Output
18 Mar 1986
19 Mar 1986
20 Mar 1986 - Casual Thursday
21 Mar 1986 - Free lunch
22 Mar 1986
23 Mar 1986
24 Mar 1986
25 Mar 1986 - Casual Tuesday
26 Mar 1986
27 Mar 1986
28 Mar 1986 - Free lunch
29 Mar 1986
30 Mar 1986 - Casual Sunday
31 Mar 1986
1 Apr 1986
format("d M Y");

    $get_day = date('d M Y', strtotime($get_day));
    $day_of_month = date('d', strtotime($get_day));
    $toChecksat = date('D', strtotime($get_day));

    if ($day_of_month == '01') {
        $first_date_of_month = new DateTime($get_day);
        echo $first_date_of_month->format('F') . "
"; echo $all = $date->format("d M Y") . "
"; } else { if ($toChecksat == 'Sat') { //Check for SATURDAY echo $all = $date->format("d M Y") . " - Free lunc" . "
"; } else if (($day_of_month % 5) == 0) { //devided by 5 $full_day_name = new DateTime($get_day); echo $all = $date->format("d M Y") . " - Casual " . $full_day_name->format('l') . "
"; } else { echo $all = $date->format("d M Y") . "
"; } } } ?>

Thursday, 16 January 2014

Error logs save in Db in Yii


1. Yii have its own method to save error logs in DB, follow the steps below in config/main.php Make new db connection (Separate db to save logs)
'log_db'=>array(
          
                        'class'=>'CDbConnection',
                        'connectionString' => 'mysql:host=localhost;dbname=db_name',
                        'emulatePrepare' => true,
                        'username' => 'root',
                        'password' => '',
                        'charset' => 'utf8',
                       
        ),
2. We can use yii default class to save logs in db "CDbLogRoute"
'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                /**/array(
                    'class'=>'CFileLogRoute',
                    'levels'=>'error, warning',
                                       
                ),
                // uncomment the following to show log messages on web pages
               
                array(
                    'class'=>'CWebLogRoute',
                ),
                           
                                array(
                    'class'=>'CDbLogRoute',
                             'connectionID'=>'log_db',   
                             'logTableName'=>'yii_logs', //log table name
                             //'categories'=>'error, warning',
                              'levels'=>'error, warning',
                             //'autoCreateLogTable'=>true,
                ),
               
            ),
        ),

3. Yii default table structure to save logs. (It will created automatically if 'autoCreateLogTable'=>true) )
CREATE TABLE IF NOT EXISTS `yii_logs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `level` varchar(128) DEFAULT NULL,
  `category` varchar(128) DEFAULT NULL,
  `logtime` int(11) DEFAULT NULL,
  `message` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
Table snapshot with real data
4. You can save custom fields in Yiilog For this you can make your own class and extend to CDbLogRoute (save in protected\components folder) Like this
createCommand()->createTable($tableName, array(
            'id'=>'pk',
            'level'=>'varchar(128)',
            'category'=>'varchar(128)',
            'logtime'=>'integer',
            'message'=>'text',
            'users_id'=>'int(11)',
            'ip'=>'varchar(50)',
            'error_code_id'=>'INT(11)',
            'output_variable'=>'varchar(225)',
            'exception_id'=>'varchar(45)',
                        'exception_category'=>'ENUM("CApplication", "CDbException", "CHttpException", "BException")',
                        'updated_date'=>'timestamp',    
                        'created_date'=>'timestamp',
            
        ));
    }
    

    
    protected function processLogs($logs)
    {

        $command=$this->getDbConnection()->createCommand();
               
                  foreach($logs as $log)
        {
                  $clog  =     explode("::", $log[0]);
                  $elog=CJSON::decode($clog[0]);
                                     
               $command->insert($this->logTableName,array(
                'level'=>$log[1],
                'category'=>$log[2],
                'logtime'=>(int)$log[3],
                'message'=>$log[0],
                                'users_id'=>Yii::app()->user->getId(),
                                'ip'=>Core::getIP(),
                                'error_code_id'=>$elog['error_code_id'],
                                'exception_id'=>$elog['exception_id'],
                                'exception_category'=>$elog['exception_category'],
                                'output_variable'=>$elog['output_variable'],
                                                       
            ));
                      
        }
    }
       



and also some change in main .php
'class'=>'application.components.ELog',
                                        'connectionID'=>'log_db',
                                        //'logTableName'=>'yii_logs',
                                        //'categories'=>'error, warning',
                                       //'levels'=>'error, warning',
                                       'levels'=>'elog',   
                                       'autoCreateLogTable'=>true,
    )

 
In controller you can call it like this
Yii::log(CJSON::encode(array('exception_id'=>00001','error_code_id'=>'1','output_variable'=>'user_id','exception_category'=>'BException'))."::", 'elog');

 

Wednesday, 1 January 2014

PHP function Returne correct IP address even if user is behind proxy

 
/**
     * Returne string correct IP address even if user is behind proxy
     * @return string correct IP address even if user is behind proxy
     * @return type
     */
    public static function getIP() {
        if (getenv('HTTP_CLIENT_IP')) {
            $ip = getenv('HTTP_CLIENT_IP');
        }
        elseif (getenv('HTTP_X_FORWARDED_FOR')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        }
        elseif (getenv('HTTP_X_FORWARDED')) {
            $ip = getenv('HTTP_X_FORWARDED');
        }
        elseif (getenv('HTTP_FORWARDED_FOR')) {
            $ip = getenv('HTTP_FORWARDED_FOR');
        }
        elseif (getenv('HTTP_FORWARDED')) {
            $ip = getenv('HTTP_FORWARDED');
        }
        else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

Friday, 6 December 2013

Yii vs Magento


Yii Magento
Category Framework Online Shop System
Web application framework

Preference (53% global votes) (43% global votes)
Website www.​yiiframework.​com www.​magentocommerce.​com
License New BSD License OSL 3.0
System requirements
Operating system Cross-platform Cross-platform
Programming language PHP PHP
Memory recommended ? 512 MB
Memory minimum ? 256 MB
More
Description Yii is a high-performance, component-based PHP framework for developing large-scale Web applications rapidly. It enables maximum reusability in Web programming and can significantly accelerate your Web application development process. Magento is a Dynamic & Robust eCommerce Platform
Tag MVC ?
Multi-user system Yes Yes
Autofocus Yes ?
Pingback Yes Yes
Extension/Plug-in Yes Conditional
Interpreter Yes ?
Database MySQL MySQL
SQLite
MSSQL
Oracle
PostgreSQL

Trackback Yes Yes
Multilingual content Yes Yes
Database model Relational Object-relational
Object-oriented

Transactions Yes ?
Unicode Yes Yes
WYSIWYG-Editor Yes Yes
Multiple projects Yes Yes
Standard compliance Yes Yes
External pages Yes No
User statistics Yes Yes
Revision control Yes Yes
Atomicity Yes ?
Isolation Yes ?
Horizontal scalable Yes No
Template language Mustache PHP
drupal
Markdown
Smarty
Twig
PHP

Target audience Beginner Web Development
SMB
Web Development
Cloud computing
Government
App developer
System Adminstration

Framework Yii Zend Framework
Full text search Yes Yes
Scaffolding Yes Yes
Design pattern Active-Record Model-View-Controller
Model-View-Controller
DAO
HMVC
Observer

Development principles Convention over configuration ?
Test-driven development
Don't repeat yourself

Difficulty level Intermediate Intermediate
Application startup time 1 ms 1 m
Version number 1.1.14 1.8
Adobe Flash Support Yes Yes
PSR-0 compliant Yes ?
Object-Relational Mapping (ORM) Yes Yes
Popularity (60% global votes) (67% global votes)
Machine Code Generation Yes Yes
Documentation level ★★★★☆ ★★★☆☆
Multi tasking Yes ?
RESTful Yes Yes
Wastebag Yes ?
Code Generation Yes ?
Dynamic typing Yes Yes
Composite keys Yes ?
Programming paradigm Object-oriented Object-oriented
Event-driven

Comments Yes Yes
Multiple categories Yes Yes
Hierarchical menus Yes Yes
Scripting language support JavaScript Prototype
PHP JavaScript
Free to use Yes Yes
Backend PHP PHP
Active Yes Yes
Database Connection Pooling No Yes
Separate Service Layer Yes ?
Web Flows Yes ?
Heroku Support No ?
Creation Date Jan-08 2008
Community Driven Excellent Basic
Wizard Yes ?
WSDL Yes ?
Reliability ★★★★★ ★★★☆☆
Versioning Good Basic
Custom queries Excellent Basic
Input Widgets Yes ?
Layout Structure Template Yes Yes
File Assets Yes Yes
API Good Good
Compiled language No ?
XML Aware Good Good
Client/Server code reuse Model-View-Controller ?
Batch Processing Yes ?
Cloud platform support Amazon EC2 ?
Amazon S3
digital ocean
ucloud biz
OpenShift
Windows Azure
Linode

Admin Generator Yes Yes
Compiler No ?
Library file size 10 MB ?
API comprehensibility ★★★★☆ ★★☆☆☆
Webmail Yes Yes
Jobs Oportunities ★★★★☆ ★★★★★
Implementation flexibility (49% global votes) (0% global votes)
Out-of-the-box functionality (53% global votes) (25% global votes)
Throttling Yes ?
Bundle system Yes Yes
Annotation Support Yes ?
LDAP Yes No
Query Cache Yes Yes
Realtime Yes ?
Data Security Yes Yes
Community feedback ★★★★★ ★★☆☆☆
Ease of use ★★★★★ ★★★★☆
Malicious Injection Prevention Yes ?
Openshift Paas Support No ?
Copy, Cut and Paste Support Yes ?
Free for commercial use Yes ?
ORM Yes ?
Realtime Server Push Yes Yes
Easy of Use Yes ?
Sexiness 100 101
Open Source Yes Yes
Web Developer Toolbar Yes ?
XQuery Support No ?
Websocket Support No ?
Maven support Yes ?
Resource File Processing Yes ?
Partial Classes Yes ?
Perfomance ★★★★★ ★★★★☆
Database migrations Yes Yes
I like it Yes Yes
Debug Mode Yes Yes
Static Typing Yes ?
Click & Edit in Place / WYSIWYG Yes ?
Autocomplete Code Yes ?
UI framewrok 8 ?
Makes you angry No Yes
Test 3 g ?
Easy to Learn Yes No
Ajax Yes ?
User management Yes ?
ORM Join Support Yes ?
Object-Oriented Views Yes ?
Object Oriented Models Yes ?
Mobile ready Yes ?
ACL Yes ?
Requests per second 851 ?
Operating system server Cross-platform ?
Package Manager Yes ?
Easy Setup Yes ?
Jquery Yes ?
Costanzia Yes ?
Grey 50 m ?
Protein ? 18
Browser support ? Firefox
Internet Explorer
Chrome
Safari