Showing posts with label Performance. Show all posts
Showing posts with label Performance. Show all posts

Monday, May 24, 2010

MySQL: Many tables or many databases?

Question:
For a project we having a bunch of data that always have the same structure and is not linked together. There are two approaches to save the data:
* Creating a new database for every pool (about 15-25 tables)
* Creating all the tables in one database and differ the pools by table names.
Which one is easier and faster to handle for MySQL?

Answer:
There should be no significant performance difference between multiple tables in a single database versus multiple tables in separate databases.

In MySQL, databases (standard SQL uses the term "schema" for this) serve chiefly as a namespace for tables. A database has only a few attributes, e.g. the default character set and collation. And that usage of GRANT makes it convenient to control access privileges per database, but that has nothing to do with performance.

You can access tables in any database from a single connection (provided they are managed by the same instance of MySQL Server). You just have to qualify the table name:

SELECT * FROM database17.accounts_table;

This is purely a syntactical difference. It should have no effect on performance.

Regarding storage, you can't organize tables into a file-per-database as @Chris speculates. With the MyISAM storage engine, you always have a file per table. With the InnoDB storage engine, you either have a single set of storage files that amalgamate all tables, or else you have a file per table (this is configured for the whole MySQL server, not per database). In either case, there's no performance advantage or disadvantage to creating the tables in a single database versus many databases.

There aren't many MySQL configuration parameters that work per database. Most parameters that affect server performance are server-wide in scope.

Regarding backups, you can specify a subset of tables as arguments to the mysqldump command. It may be more convenient to back up logical sets of tables per database, without having to name all the tables on the command-line. But it should make no difference to performance, only convenience for you as you enter the backup command.

-- Bill Karwin (the author of SQL Antipatterns from Pragmatic Bookshelf)

Tuesday, May 12, 2009

Non-blocking JavaScript

Include via DOM

var js = document.createElement('script'); 
js.src = 'myscript.js'; 
var h = document.getElementsByTagName('head')[0]; 
h.appendChild(js);

Non-blocking JavaScript
  And what about my inline scripts?
  Setup a collection (registry) of inline scripts

Step 1
Inline in the <head>:
var myapp = { 
   stuff: [] 
}; 

Step 2
  Add to the registry
Instead of:
  alert('boo!');
Do:
  myapp.stuff.push(function(){ 
    alert('boo!'); 
  );

Step 3
  Execute all

var l = myapp.stuff.length;  
var l = myapp.stuff.length;  
for(var i = 0, i < l; i++) { 
myapp.stuff[i](); 

Premature optimization

Knuth: The premature optimization is the root of all evil.

Crockford: Make it right before you make it fast.

Wednesday, August 13, 2008

javascript 闭包妙用

在javascript中,程序是一行行执行的,在上一行返回结果后执行下一行的语句。如果在执行一条要耗时的操作,而接下来的程序操作与此条执行语句并无关系,则可将此语句放在一个闭包里去执行,用setTimeout(function(){}, 0)来激活此闭包运行,这个技巧可用于加速页面渲染,同时对多个dom对象操作。


<html>
<head>
<title>Yield demo</title>
<script type="text/javascript">
<!--
function doSave(id, doYield) {
document.getElementById(id).innerHTML = '<span style="font-style: italic;">Saving...</span>';
if (doYield) {
var startTime = (new Date()).getTime();
setTimeout(function() { doSaveImpl(id); }, 0); // runing in clousres
var endTime = (new Date()).getTime();
alert("yield time is:" + (endTime - startTime));
} else {
var startTime = (new Date()).getTime();
doSaveImpl(id);
var endTime = (new Date()).getTime();
alert(endTime - startTime); // firebug console.log()
}
}
function doSaveImpl(id) {
var numIters = 10000000;
for (var i = 0; i < numIters; i++) {
var j = Math.sqrt(i); // slow operation
}

document.getElementById(id).innerHTML = '<span style="color: #090">Saved!</span>';

setTimeout(function() { reset(id); }, 3000);
}
function reset(id) {
document.getElementById(id).innerHTML = 'Ready';
}
//-->
</script>
<style type="text/css">
.button {
border: 1px solid black;
background: #ffff9f;
padding: 5px;
font-size: 14pt;
color: black;
text-decoration: none;
-moz-border-radius: 8;
}
.status_msg {
color: #999;
margin-top: 0.5em;
font-size: 18pt;
}
</style>
</head>
<body>

<h1>Yield Demo</h1>

<h2>Without yielding:</h2>

<a href="javascript:;" onclick="doSave('noyield', false); return false;" class="button">Save</a>
<div id="noyield" class="status_msg">Ready</div>

<h2>With yielding:</h2>

<a href="javascript:;" onmousedown="doSave('yield', true); return false;" class="button">Save</a>
<div id="yield" class="status_msg">Ready</div>

</body>
</html>

Reference: http://josephsmarr.com/oscon-js/yield.html