Category Archives: Programming

A few mysql handy commands and useful tips to optimize speed

Show table schema and execution plan:
show create table
explain select

Index manipulation:
show index on drop index on create index on

show the running process:
show processlist

Also from stackoverflow:

Do you have an index on key_1?

Without it, each query by itself will already be slow just looking for 123.
Do you have an index on (key_1, key_2)?

Because select distinct key_2 where key_1 = 123 is really fast if it can get all the necessary data from the index alone. No need to access the table.
Are the rows/indexes fixed-size?

Traversing a fixed-size table/row can be faster because one always knows where the x-th record is by just calculating the offset. Variable row sized tables are slower.
Have you tried adding an autoincrement surrogate primary key?

Indexes work way better when all they have to store is the column, and a small primary key. Composite primary keys are slower.
Did you consider a read-only table?

You can pack myisam table for fast access, but they become read-only. It’s a hack that has its uses though.
One step further, have you considered a datawarehouse?

If the tables don’t change often, it might be best to duplicate the information for fast access.

Can you post a show create table statement? Seeing the columns and indexes would help. Can you post an explain select statement? Seeing which indexes are used would help.

get rid of weird #text when you load a xml document into php DOMDocument

When you load a xml file into DOMDocument in php, you can encounter some weired #text node during the iteration. I searched around, and found this solution, just put a line of code like below:
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;

And then you are done!

And last not least this is where I found the answer, it gives a thoroughly explanation:

download with urllib and got exception: UnicodeEncodeError: ‘charmap’ codec can’t encode character

When you are download html with urllib or something alike, if you have this message:

File “C:\Python33\lib\encodings\”, line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: ‘charmap’ codec can’t encode character

Chances are you are print() out some unsupported char most likely in utf-8 in the sys.stdout, but your sys.stdout console is default to cp437.
A fix is put a line like this before the print code as here:
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,'cp437','backslashreplace')

besides you can also use utf-8 as the encoder:
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,'utf-8')

Solution to missing mkstemp when you are compiling under mingw

I googled around, doesn’t find a perfect solution. But I do come across 2 files on apple website, they actually implement the missing functions while dealing with the code ported to mingw.

For my scenario, I simply created a compiler based branch:
#if defined(__MINGW32__)

int mkstemp(char *template)
char *filename = mktemp(template);
if (filename == NULL)
return -1;
return open(filename, O_RDWR | O_CREAT, 0600);


I also downloaded and attached here in case could be useful later:

Datetime::Now is high precision time

You can get the high precision time with Datetime::Now, instead of with complicated: QueryPerformanceCounter
The test code is:

for (int i=0; i< 20; i++) { Console::WriteLine(DateTime::Now.Ticks); }

And this is the output:
datetime now ticks
It is precise to the 100 nanosecond as you can see from the above image.

Quick reference code to convert C# RectangleF to Rectangle

Quick code here for you to copy and paste in your work, so save you from some typing:

public static Rectangle ConvertToRectange(RectangleF rectanglef)
return new Rectangle(

a daily life issue with svn

First of all, I just want to describe the problem here, I don’t have an answer.
Svn is a great tool to keep the version of changes, despite of all the greatness, there is an inconvenient issue when you committed to put it into heavy daily use. It doesn’t do well in this specific scenario:
If you want to rename a file, move it to a different folder or delete a file, you can easily do it in your ide or file explorer, just 1 or 2 clicks of mouse away. But then the svn lost the track of these file name based changes.
On the other hand, even when you feel the need to do these file name based changes, you operate on the svn directly, then you will make the project specific solution file outdated, in the case of C#, it is csproj.

In both cases, you have to redo the work manually on the other one to get updated.

Hopefully, there could be a solution here.

Maybe, a way to go is to let tortoiseSVN monitor the changes relate to the file name, then it adds the commands of changes to the pending commit list, the same way as it deals with the content changes inside a file.

compile lame as dll from command line with nmake

here is the command line without nasm:
d:\source\lame-3.99.5>nmake libmp3lame.dll -f makefile.MSVC CPU=P3 asm=no

If you have the nasm installed: (good to have the asm optimization)
d:\source\lame-3.99.5>nmake libmp3lame.dll -f makefile.MSVC CPU=P3

and nasmw is now nasm in case you are looking for it but failed. Simply rename it if it is really needed.