Monday, September 23, 2013

PHP Mysql Fetch Association Vs Fetch Array Vs Fetch Object Performance Analysis

In this analysis we will see how the 3 main Mysql data fetching methods in PHP performance under different loads
In most programming situations regarding php-mysql you will need to fetch data from the db to  populate your application with data
There are 3 most popular methods of getting more than one data when fetched.
  • mysql_fetch_assoc() : This gets you an associative array of data.
  • mysql_fetch_array() : This returns a combination array of associative elements as well as data with numerical index.
  • mysql_fetch_object() : Returns an object with properties that correspond to the fetched row.
We are comparing the 3 methods here based on benchmark tests done by spearhead softwares on various load conditions.
In the test we run each methods result dataset containing 10,100,1000,10000,100000,1000000,10000000 data and see how fast and efficient each function operates.
Each data set contains a unique id element, int & a string element which makes it the normal set of values which is being retrieved in most normal cases.
PHP mysql fetch function performance benchmarksSource: SpearheadSoftwares.commysql_fetch_objectmysql_fetch_arraymysql_fetch_assoc10100100010000100000100000010000000-505101520253035Seconds

From this we can clearly see that the default functionality of mysql_fetch_assoc is running at a higher performance than the other 2 methods (mysql_fetch_array & mysql_fetch_object). mysql_fetch_object performance is justified as it returns objects instead of native arrays which will always bring in a better memory usages than any other kinds of output array. And for using the Object oriented result set approach the other 2 functions cannot satisfy. Lets take the case of mysql_fetch_array(); The problem here is in daily programming usages many php programmers have used the mysql_fetch_array() as such without going into the details of its usage. By default mysql_fetch_array returns 2 types of results sets as given by its default result_type value MYSQL_BOTH

  • result_type : MYSQL_NUM - which gives us only a numbered index array eg: array([0]=>'apple')
  • result_type : MYSQL_ASSOC - which us only associative array eg: array('fruit'=>'apple') where 'fruit' will be the database column name or alias used.
So when we use mysql_fetch_array we get a duplicate set of data consisting of  both numbered index as well as associative arrays which ofcourse is our performance blocker.
So when ever you are using mysql_fetch_array function always specify the return_type of the result set array needed.
And in most cases we will need an associative array during iterative data management in php so the best function to call will be as shown in the graph ,performance wise -mysql_fetch_assoc()
Hope you liked our article. If you have more ideas regarding performance in this category, do comment .

Thursday, September 19, 2013

How to Transfer Files Using PuTTY - PSCP


PuTTY is the CIT-recommended application for secure file transfer using SCP between Windows clients and Windows or Unix servers.
PuTTY is available from PuTTY.org.

Install PuTTY and Set up a Server Connection

PuTTY is the recommended SCP client for Windows client and Unix server file transfers at Cornell. PuTTY is available from PuTTY.org.
  1. Download the pscp.exe file from PuTTy.org. Click the file name and save it to your computer.
    If you want a PuTTY shell program, download and install putty.exe as well.
You are now connected to the server and can begin transferring files.

Transfer Files Using PuTTY

  1. Open a Windows command line window. On the Start menu, click Run.

    filewindowsstart
  2. In the Open box, type cmd, and then click OK.

    filewindowscmd


Add putty scp client( pscp.exe) and (putty.exe) path to windows Environment Variable

MS Windows uses a concept of paths to go searching for where programs are located on your hardware. When using the DOS box, the operating system has no context about where your programs are located on the hard drive. When using the GUI, the information about where the program is located, is contained in the Shortcut that you are clicking on.
You could start PuTTY by typing something like this:
PuTTY-cli-10.jpg
but that is too much typing for me.
We can tell Windows that the directory where the PuTTY program is and to look in there for the program as needed.

To the Control Panel System tab then

In the Control Panel, System Properties, select the Environment Variables button at the bottom:
PuTTY-cli-11.jpg
and then select Path in the System Variables section, and press the Edit button.
PuTTY-cli-12.jpg
PuTTY-cli-13.jpg
and it will look like this ( you need to be a little bit careful here not to delete the content, if you think you have mis-types then just press escape and start over), on the end of the line enter the following text exactly (including the semi colon):
;C:Program FilesPuTTY
PuTTY-cli-15.jpg
This set the path variables to include the PuTTY program directory in the search path. You only have to do this once. Windows will set the the Environment Variable permanently from now on.
You will need to logout at this point, or restart. The Path Variables are only read once at boot time.
When you come back, open the DOS box again (Ctrl-Escape, R, cmd, Enter – just to remind you), and type path to confirm the variables are in place.
PuTTY-cli-17.jpg



  1. At the Windows command prompt, enter:
    pscp source_file_name userid@server_name:/path/destination_file_name.

    For example:
    c:\>pscp june06extract wrhse@warehouse.cit.cornell.edu:/mydata/june06extract.

  2. When prompted, enter your password for the server.
Note: Complete documentation for PuTTY is on the PuTTY.org site.


Obtaining and Using PSCP

PSCP is a freeware SCP (Secure CoPy) program for the Windows command line processor. You can use this program instead of FTP for copying files to or from the Unix servers at the NBER offices. FTP is not allowed from clients outside nber.org to servers inside the firewall because FTP leaves passwords in plain-text and vulnerable to eavesdropping. PSCP should work with any host supporting SSH.

Details:

  • Download PSCP here.
  • Save pscp.exe somewhere on your hard drive. C:\windows\ is a good location in the default execution path.
  • The command line syntax is:

    • pscp [options] [user@]host:source target
    • pscp [options] source [source...] [user@]host:target

      Options:
      -p preserve file attributes
      -q quiet, don't show statistics
      -r copy directories recursively
      -v show verbose messages
      -P port connect to specified port
      -pw passw login with specified password

      source or target may be a pathname or a ~userid/filename (on the Unix side). You will be prompted for the password of `user'. This is an old-fashioned command line program - you need to open a DOS window to run it.
Copying files from your UNIX host to your Windows PC.

  • In order to copy a file from your UNIX host to your Windows PC your command line would look like this:

    • pscp username@yourhost:file.foo c:\temp\file.foo
    • This command line copies "file.foo" located in your home directory on your UNIX host to the temp directory on your C:\ drive.
Copying files from your Windows PC to our UNIX host.

  • In order to copy a file from your Windows PC to your our Unix host your command line would look like this:

    • pscp -v c:\"my documents"\file.foo username@nber.org:file.foo

    • This command line copies "file.foo" located in the "my documents" directory on your C:\ drive to your home directory on our Unix cluster.
    • Remember: Backslashes (\) for your DOS and Windows files and forward slashes (/) for your UNIX files.