Importance Of Logging In MySql

Enable logging in MySql is necessary to manage your server and helps in analyzing performance and investigating problems.

Most of the time when we find any issues in our web application or when user complains about the performance then only we print our query and run that query in MySql server to analyse what is wrong.

Prevention is better than cure.


Important Log Types In MySql:

  • The error log
  • The query log
  • The slow query log

The error log:

If you enable error log in MySql server it keeps record of each error occurs in the MySql server. You can check MySql configuration file(Ex: my.cnf in /etc/mysql  in Ubuntu) whether error log is enabled or not.

    # Error log - should be very few entries.
    log_error = /var/log/mysql/error.log

Once you change the configuration restart MySql server to reflect the changes made for error  log and then find your MySql errors in var/log/mysql/error.log.

The query log:

The query log is very important log also as it tracks every query executed in your MySql Server for your application. It also displays details about which clients are connected to the server and what these clients are doing.

We always emphasise on slow queries but redundant/multiple queries are worse than slow queries.

If you have slow query running for your application then it needs to be refactored for your application for better performance. But if you are not aware of how many queries are running for the feature for your application or there might be chance that duplicate queries are executing through different functions for same feature but you have never got chance to check.

Then query log helps to find out all queries are running for your feature so you can identify redundant query or multiple queries which can be optimised.

Enable query log(Ex: my.cnf in /etc/mysql  in Ubuntu) –

    general_log_file        = /var/log/mysql/mysql.log
    general_log             = 1

After enable the query log make sure to restart your MySql server to reflect the changes. Once you are done then you can just read/var/log/mysql/mysql.log or use the command tail -f /var/log/mysql/mysql.log and browse your feature in application to see how many queries are getting executed for that feature.

The slow query log:

The slow query log lists all queries that takes more than amount of time mentioned in long_query_time variable in configuration file. You can modify long_query_time according to your requirement. Please check the configuration for this setting:

    # Here you can see queries with especially long duration
    #log_slow_queries	= /var/log/mysql/mysql-slow.log
    #long_query_time = 2
    #log-queries-not-using-indexes

Any query that takes longer than long_query_time will be listed in mysql-slow.log file.

Conclusion:

Use the MySql logs to make your application efficient and robust.

Stay tuned for more type of logs in MySql.

Thanks for your valuable time to read the blog and if you like don’t forget to like and share the blog. Comments are always welcome 🙂


 

 

Advertisements

Improve the way of writing functions!

Even though we write lot of functions daily we hardly pay attention how to improve our functions.

FUNCTIONS SHOULD DO ONE THING. THEY SHOULD DO IT WELL. THEY SHOULD DO IT ONLY.

Doing Many Things At A Time!

Before giving attention to write an effective function you need to know what are the main points you need to take into considerations.

  • Small!
  • Blocks and Indenting
  • Do One Thing
  • Reading Code from Top to Bottom: The Stepdown Rule 
  • Use Descriptive Names
  • Function Arguments
  • Duplication

Small!

Function should be small. If you are thinking how small they can be, then I would say 2 lines or 3 lines or 4 lines long but these number of lines are not fixed though. You may consider these line numbers as a checkpoint like if your function has many lines make sure your function is not doing so many things.

Below example is from clean code book can be taken as reference:

    public static String renderPageWithSetupsAndTeardowns(
        PageData pageData, boolean isSuite) throws Exception {
        if (isTestPage(pageData)) {
            includeSetupAndTeardownPages(pageData, isSuite);
        }
        return pageData . getHtml();
    }

Blocks and Indenting

Blocks within if statements, else statements, while statements, and so on should be one line long. Probably that line should be a function call. Not only does this keep the enclosing function small, but it also adds documentary value because the function called within the block can be a nicely descriptive name.

This also implies that function should not be large enough to hold nested structures. Therefore, the indent level of a function should not be greater than one or two. This, of course makes the functions easier to read and understand.

Do One Thing

Your function may have one line of code but doing many things at that line is not a efficient and optimised function.

For Ex:

return level4 != null ? GetResources().Where(r => (r.Level2 == (int)level2) && (r.Level3 == (int)level3) && (r.Level4 == (int)level4)).ToList() : level3 != null ? GetResources().Where(r => (r.Level2 == (int)level2) && (r.Level3 == (int)level3)).ToList() : level2 != null ? GetResources().Where(r => (r.Level2 == (int)level2)).ToList() : GetAllResourceList();

Similarly another example:

function build_address_list_for_zip($zip) {
    $query = "SELECT * FROM ADDRESS WHERE zip = $zip";
    $results = perform_query($query);
    $addresses = array();
    while ($address = fetch_query_result($results)) {
        $addresses[] = $address;
    }

    // now create a nice looking list of
    // addresses for the user
    return $html_content;
}

In this function you are fetching record from database and generating html content and rendering it. So it can be split into two functions:

function fetch_addresses_for_zip($zip) {
    $query = "SELECT * FROM ADDRESS WHERE zip = $zip";
    $results = perform_query($query);
    $addresses = array();
    while ($address = fetch_query_result($results)) {
        $addresses[] = $address;
    }
    return $addresses;
}

function build_address_list_for_zip($zip) {
    $addresses = fetch_addresses_for_zip($zip);

    // now create a nice looking list of
    // addresses for the user
    return $html_content;
}

Reading Code from Top to Bottom: The Stepdown Rule

We normally read like a top-down narrative. So when you are calling function inside another function definition then make sure to define the calling function below the function where it is called instead on top of that function:

Ex:

function getStudentInformation() {
    $this->getName($studentId);
    $this->getAge($studentId);
}

function getName($studentId){
    // return student name
}

function getAge($studentId){
    // return student age
}

It should not be like this:

function getName($studentId){
    // return student name
}

function getAge($studentId){
    // return student age
}

function getStudentInformation() {
    $this->getName($studentId);
    $this->getAge($studentId);
}

Use Descriptive Names

Don’t be afraid to make a name long. A long descriptive name is better than a short name which is difficult to understand.

A long descriptive name is better than a long descriptive comment.

Use a naming convention that allows multiple words to be easily read in the function names, and then make use of those multiple words to give the function name that says what it does.

Don’t be afraid to spend time choosing a name. Indeed, you should try several different names and read the code with each in place. As modern IDEs like Visual Studio, PhpStorm, JetStorm makes it trivial to change function names easily.

Function Arguments

The ideal number of arguments for a function is zero (niladic). Next comes one (monadic), followed closely by two (dyadic). Three arguments (triadic) should be avoided where possible. More than three (polyadic) requires very special justification—and then shouldn’t be used anyway.

If your function is having more number of arguments then your function is doing one thing at a time. So check your code and split the functions doing one thing at a time.

Duplication

Duplication may be the root of all evil in software. Many principles and practices have been created for the purpose of controlling or eliminating it.

If you are writing large functions doing many things then there is more chance of having code duplication in different functions doing same thing which can be refactored by creating small functions. That small function can be invoked in different functions.


Conclusion:-

So check the above points to write an effective and efficient function which is easy for maintenance and can be tested well.

Hope you enjoy reading the blog and please like and share if you like it and what can be improved don’t hesitate to comment 🙂

Stay tune for more on clean and quality code writing.

References:-  

Robert C.Martin Series: Clean Code Book

https://stackoverflow.com/questions/475675/when-is-a-function-too-long