Skip to main content

Posts

SQL query optimization

SELECT query {EXPLAIN | DESCRIBE | DESC}  We usually use this command to check to see if how many rows the query goes through. Especially for the INDEX column checking. Search LIKE '%key%' consumes more resource than LIKE 'key%' Denormalisation This database design helps to speed up the data reading. Because the SQL query no need to join data between tables.

Some popular design patterns

Singleton design pattern This pattern makes sure only one object is created and used though out the system. Create a static instance in the single class public class SingleObject { //create an object of SingleObject private static SingleObject instance = new SingleObject (); //make the constructor private so that this class cannot be //instantiated private SingleObject (){} //Get the only object available public static SingleObject getInstance (){ return instance ; } public void showMessage (){ System . out . println ( "Hello World!" ); } } Use this instance through the system. public class SingletonPatternDemo { public static void main ( String [] args ) { //illegal construct //Compile Time Error: The constructor SingleObject() is not visible //SingleObject object = new SingleObject(); //Get the only object available SingleObject object = S...

Sub-program (procedure and function) in SQL

What is sub-program in SQL? Sub-program is a programming unit which is created in a database to perform some tasks on data. Basic usages of sub-program? Create CREATE PROCEDURE or CREATE FUNCTION Delete DROP PROCEDURE or DROP FUNCTION Two kinds of sub-program: PROCEDURE: perform an action but NOT return a value, but we can get output value by OUT variables. and FUNCTION: compute and return a value Create procedure: CREATE [ OR REPLACE ] PROCEDURE procedure_name [( parameter_name [ IN | OUT | IN OUT ] type [, ...])] { IS | AS } BEGIN < procedure_body > END procedure_name ; Create function: CREATE [ OR REPLACE ] FUNCTION function_name [( parameter_name [ IN | OUT | IN OUT ] type [, ...])] RETURN return_datatype { IS | AS } BEGIN < function_body > END [ function_name ]; What is cursor? A cursor holds the rows (one or more) returned by a SQL statement. Triggers are stored programs, which are automatically executed or fired wh...

Fix cross domain in ajax request

Follow this sample ajax request $.ajax({    "type": 'POST',    "url": url,     "crossDomain": true,    "data": params,    "dataType": 'json',    "async": false,    "success": function(data){ done_fn(data, is_test);    },    "error": function (jqXHR) { var data = $.parseJSON(jqXHR.responseText); $("#results").text(data.message);    } }); The keys are crossDomain: true and async: false Hope to help many people.

Jenkins: SSH connect with username and password to AWS Linux instance

Jenkins  2.46.1 does not support to have a ssh connection with default pem file from aws. To do the automatically deployment from Jenkins server, a ssh connection should be establish from Jenkins server to the instance. These are steps to do to enable the SSH connection: AWS instance: Create a new user on the instance Edit the SSH config file to enable Password authentication for SSH and especially, specify some special encryption algorithms that supports Jenkins  File: /etc/ssh/sshd_config Put this at the end of the file KexAlgorithms diffie-hellman-group1-sha1,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1 Use the new created user for Jenkins  

New features in PHP 7

1. Performance   PHP 7 helped to reduce the memory usage and increase the performance. Some benchmarks show that the new performance is as twice as PHP 5.6. PHP 7 can handle 2580 requests per second comparing to 1400 requests per second (Drupal). In my case, the wordpress site of our company needed 25s to fully load, after I apply PHP 7, the loading time is only 8s. Why is PHP 7 faster? + New core engine PHP 4 uses Zend Engine (not confused with Zend framework) PHP 5 uses Zend Engine II PHP 7 uses PHPNG, a refactored Zend Engine II The refactoring makes the new generation of engine consume less memory (bucket size, HashTable size, etc), less CPU resource. 2. Declare type for variables In PHP 5 does not require developers to declare type. Type declaration helps developers to get the expected result. But in PHP 5, dev also can declare type using Type hinting, however, this is limited to function parameters declaration only and also limited to Class type ...

Recommender engine - Part 2: How does Recommendation work and how to use it?

3. How does it work? Content-based recommendation User-behaviour-based recommendation  4. How to apply it to your website?  Third-party service  Do it your own, coursera website  https://articles.uie.com/recommendation_systems/ http://fortune.com/2012/07/30/amazons-recommendation-secret/ https://www.quora.com/How-much-sales-lift-is-attributed-to-Amazons-recommendation-engine http://glinden.blogspot.com/2006/12/35-of-sales-from-recommendations.html https://articles.uie.com/recommendation_systems/ https://www.quora.com/What-are-the-benefits-of-using-product-recommendations-on-my-e-commerce-site

Recommender engine part 1: What it is and why you need it

Websites of all types should consider these before discounting the use of a recommender engine. The future of the web is helping people shop and experience sites in a uniquely personal way for them; whether the website is e-commerce, media, social network, or search engine, this is a win-win situation for both the users and the site owners (or store owners, in the case of e-commerce). One way to do this? Utilising a recommender engine. A recommendation engine, also known as a recommender system, is software that analyses available data (user behaviours, product properties, etc) to make suggestions for something that a website user might be interested in, such as a book, a video or a job, among other possibilities. Amazon was one of the first sites to use a recommendation system. Recommended product from Amazon website. Websites have a lot to gain in using a recommender engine, including: Maybe change Search is hard for users, discovery/exploration is easy to...

PHP json_encode return empty array [] instead of empty object {}

Problem: Get below array for example.  $status = array ( "message" => "error" , "club_id" => $_club_id , "status" => "1" , "membership_info" => array (), ); This array will be encoded in json format echo json_encode($status); This function return json: {"message":"error","club_id":275,"status":"1","membership_info": [] } Notice the empty array [], it is not consistent, it should be an empty object {} {"message":"error","club_id":275,"status":"1","membership_info": {} } The cause: This problem is caused by the called function array(), which yields an empty array [] Solution: There are 2 solutions for this issue: Solution 1: Call new stdClass instead of array(), stdClass generates an empty object {} Solution 2: The above solution is complex in case there are plenty of arr...

Rich - poor divide in Vietnam

There is a major rich-poor divide in Vietnam these days: people in high-income areas continue to have a much better chance of living longer than those in low-income area. For example, a boy born in 2012 in a high-income area can expected to live to the age of around 76, which is 16 years longer than a boy born in a low-income area. For girls, the gap is wider - 19 years separates life expectancy in high-income (82 years) and low-income areas (63 years). So there is a corresponding uptick in life expectancy if the income is higher.

Recomender system

In next e-commerce project at Softfoundry, I would like to implement the recommendation system which recommend the suitable products for customers. In my thesis of the university, I also experienced with the recommendation algorithm and tried to implement some of them. It must be a fascinating topic to talk about.

Command Prompt VS 2010: Fix MSVCRT.lib(MSVCR100.dll) : error LNK2005: xxx already defined in LIBCMT.lib(xxx.obj)

One of my task is to use Command Prompt of Visual Studio 2010 to evaluate the assignments of students. This assignment is about pointer and memory management in C++. The students are required to use new and delete operator in C++ to manage the memory and they need to ensure that the number of new callings must be equal to the number of delete callings. I overload the new and delete operator, build it into a static library (.lib file) and require student to use this .lib file when implementing the assignment. There is no problem when building the .lib file in VS 2010 project, but there are errors when compile the source code if I link the source code to the above static .lib file. Here is the format of error: MSVCRT.lib(MSVCR100.dll) : error LNK2005: xxx already defined in LIBCMT.lib(xxx.obj) Fortunately, I found 2 ways to fix this error on the internet. The first one is to remove the default lib when compiling the source code, it's libcmt.lib, because this libcmt.lib alread...

HTTP_REFERER

A friend of mine ask me if a server can know where we come from before we go to that server (referal link). YES. This information is the field "HTTP_REFERRER" in $_SERVER of the coming request. Here is an example: http://113.161.96.198/referal/ For the reason of SEO, some guys do not want any server know about this referal link that points to their own server. Here are some solutions: HTML5: Add norefer attribute   No REFERRER PHP redirect: <?php header( 'Location: http://113.161.96.198/referal/ ' ) ; ?> .... lot of solutions lol

Create openvpn certificate remotely

VPN (Virtual Private Network) is using in my management system. As a requirement, the administrator can create the certificate (crt file) for VPN client remotely, the information as well as the files of these certificates should be stored in database so that the admin can download them from web UI. I installed the open source OpenVPN, this tool is simple but robust enough to provide the access from the server VPN to client VPN. However, the disadvantage is that you can only use terminal to generate the crt file :(. Here are some solutions but I suppose none of them is acceptable. The first solution is to using ssh to remote access the VPN server then manually create the crt files. Also, create a service to scan the keys folder of VPN to update these files into database. This solution is the easiest way to do but there is no provision of user friendly. The second solution is to write an bash script file which contains all the manual command to yield the certificate. However, how to ...

Calculate the execution time of multi-thread program

Indeed, multiple-thread program helps improve the performance of the application. One of the best ways to test if the performance is really improved is the execution time. In Java,  System.currentTimeMillis();  is usually used to calculate the execution time of a code block. long startTime = System.currentTimeMillis(); <code block here> long stopTime = System.currentTimeMillis(); long elapsedTime = stopTime - startTime; But if the code block contains multiple threads inside then this calculation is not true. Usually, the elapsedTime will be calculated before the longest thread completed, as a result, the elapsedTime will be incorrectly calculated. To cope with this problem, CountDownLatch is a simple tool to be used. The general idea is that this object will set a countdown number, called stopWatch , equal to the number of threads. In run() function of each thread, stopLatch is coutdowned. Outside of the code block, stopWatch calls await function, which m...