Category Archives: Chỉ dẫn lập trình

Config IP for using laptop at many places

Create new file config-ip.bat

REM @author: huyphungvan
REM @date: 8:48 PM 8/27/2009
@echo off
set /P loc=”Config IP Address For Home Or Offcie [H/O]? ”
IF “%loc%”==”H” GOTO HOME
IF “%loc%”==”O” GOTO OFFICE
ECHO H or O, please.
PAUSE
EXIT

:HOME
netsh interface ip set address local static 192.168.1.114 255.255.255.0 192.168.1.1 1
REM netsh int ip add dns “Local Area Connection” 208.67.222.222 index=2
GOTO END

:OFFICE
netsh interface ip set address local static 192.168.0.114 255.255.255.0 192.168.0.253 1
REM netsh int ip add dns “Local Area Connection” 208.67.222.222 index=2
GOTO END

:END
pause

Copy to Start up folder (C:\Documents and Settings\All Users\Start Menu\Programs\Startup)

Remove carriage return (CR) character

Linefeed characters are represented as ordinal 10, or hexadecimal 0x0A
Carriage returns characters are represented as ordinal 13, or hexadecimal 0x0D

File In *nix: Lines end with a single linefeed (LF) character
File In Windows: Lines end with the carriage return – linefeed combination (CRLF)

Converting file from Windows to *nix means that remove carriage return character
tr -d “\r” < windows.txt > linux.txt

SQL Performance Tips

Specific Query Performance Tips:
1. Use EXPLAIN to profile the query execution plan
2. Use Slow Query Log (always have it on!)
3. Don’t use DISTINCT when you have or could use GROUP BY
4. Insert performance
1. Batch INSERT and REPLACE
2. Use LOAD DATA instead of INSERT
5. LIMIT m,n may not be as fast as it sounds
6. Don’t use ORDER BY RAND() if you have > ~2K records
7. Use SQL_NO_CACHE when you are SELECTing frequently updated data or large sets of data
8. avoid wildcards at the start of LIKE queries
9. avoid correlated subqueries and in select and where clause (try to avoid in)
10. no calculated comparisons — isolate indexed columns
11. ORDER BY and LIMIT work best with equalities and covered indexes
12. separate text/blobs from metadata, don’t put text/blobs in results if you don’t need them
13. derived tables (subqueries in the FROM clause) can be useful for retrieving BLOBs w/out sorting them. (self-join can speed up a query if 1st part finds the IDs and use it to fetch the rest)
14. ALTER TABLE…ORDER BY can take data sorted chronologically and re-order it by a different field — this can make queries on that field run faster
15. Know when to split a complex query and join smaller ones
16. Delete small amounts at a time if you can
17. make similar queries consistent so cache is used
18. Have good SQL query standards
19. Don’t use deprecated features
20. Turning OR on multiple index fields (<5.0) into UNION may speed things up (with LIMIT), after 5.0 the index_merge should pick stuff up.
21. Don’t use COUNT * on Innodb tables for every search, do it a few times and/or summary tables, or if you need it for the total # of rows, use SQL_CALC_FOUND_ROWS and SELECT FOUND_ROWS()
22. Use INSERT … ON DUPLICATE KEY update (INSERT IGNORE) to avoid having to SELECT
23. use groupwise maximum instead of subqueries
Continue reading →

PHP script download Youtube clip

<?php
    error_reporting(0);
    /*
    @author: huyphungvan
    @date: 10:30 AM 8/23/2009
    @customer: diendantiengiang.com
    */
    function downloadFLV($vid = ‘5-d2eZrDcns’) {
        if (empty($vid)) return false;
        $url = ‘http://www.youtube.com/get_video_info?&video_id=’ . $vid;
        $source = file_get_contents($url);
        $source = urldecode(urldecode($source));
        $head = strpos($source, “|”);
        if ($head === false) return false;
        $tail = strpos($source, “|”, $head + 1);
        if ($tail === false) return false;
        $flv = substr($source, $head + 1, $tail – $head);
        $pos = strrpos($flv, “,”);
        $flv = substr($flv, 0, $pos);
        // echo $source;
        // echo “<hr />”;
        return $flv;
    }
    // URL: /download.php/9_AoAgrgZ70
    
    $vid = $_SERVER[“PATH_INFO”];
    $vid = substr($vid, 1);
    
    $flv = downloadFLV($vid);
    if (!empty($flv)) header(“Location: $flv”);
    else {
        header(“HTTP/1.0 404 Not Found”);
        echo ‘File not found’;
    }
?>