html footer at the bottom

I want the footer of an html page to never be higher than the bottom of the browser window. In other words, if there’s not enough content to fill a webpage, I want the footer to be at the bottom of the page (rather than hovering underneath the content). See this example. Assuming a standard […]

database indexes and optimization

Previously, I discussed database schema normalization. Next, I would like to add indexes and optimize a schema for high-volume production usage. Consider the following schema: CREATE TABLE foobar_users ( user_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, username VARCHAR(20) NOT NULL ); CREATE TABLE foobar_groups ( group_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, groupname VARCHAR(20) NOT NULL ); […]

php DB adapter, decorator

Previously, I created a php database adapter that calls mysql stored procedures as if they were local php functions. Now I want to refactor each of the features of the DB adapter into a decorator design pattern. The core database adapter encapsulates the connection config and provides lazy-loading connections for both write-master and read-slave. I […]

php DB adapter, revisited

I would like to call mysql stored procedures as if they were local php functions. For example, let’s use the following mysql stored procedure, DELIMITER // CREATE PROCEDURE `usersByPage`(page_num INT, per_page INT) BEGIN SET @lim_start = (page_num – 1) * per_page; SET @lim_end = per_page; PREPARE stmt FROM ‘SELECT * FROM foobar_users ORDER BY user_id […]

mysqldump, tips and tricks

I want to backup a mysql database. The easiest approach is using mysqldump with its default options, i.e., # mysqldump -u user -h host -ppass db > backup.sql This will dump the full DDL and DML needed to re-create the database. Table locking is enabled by default, although for InnoDB it is recommended to use […]

Denormalization?

Previously, I normalized my tables to Sixth Normal Form (6NF). Now, I want to consider denormalizing, if and when it’s appropriate. Denormalizing is different than an un-normalized schema, which is never, ever, recommended. Strategies for denormalization appear in data-warehousing designs, specifically in OLAP star schemas. However, a snowflake schema is often a viable (and normalized) […]

Normalization, 6NF

Previously, I normalized my tables to Fifth Normal Form (5NF). Now, I want to continue to normalize such that the tables in my schema are in Sixth Normal Form (6NF). 6NF requires that each table satisfies only trivial join dependencies. All of the 5NF tables are already in 6NF, except one (it’s been going unnoticed […]

Normalization, 5NF

Previously, I normalized my tables to Fourth Normal Form (4NF). Now, I want to continue to normalize such that the tables in my schema are in Fifth Normal Form (5NF). 5NF requires that every join dependency in a table is implied by the candidate keys. All of our 4NF tables meet the 5NF requirement, but […]

Normalization, 4NF

Previously, I normalized my tables to Third Normal Form (3NF) and Boyce-Codd Normal Form (BCNF). Now, I want to continue to normalize such that the tables in my schema are in Fourth Normal Form (4NF). 4NF requires for any non-trivial multivalued dependencies X->Y, that X is a superkey. In other words, there are not multiple […]

Normalization, 3NF

Previously, I normalized my table to Second Normal Form (2NF). Now, I want to continue to normalize such that the tables in my schema are in Third Normal Form (3NF). 3NF requires that every non-prime attribute is directly dependent on every candidate key. Fortunately, normalizing to 2NF accomplished this for all but one table, i.e., […]

Normalization, 2NF

Previously, I normalized my table to First Normal Form (1NF). Now, I want to continue to normalize such that the tables in my schema are in Second Normal Form (2NF). 2NF requires that all non-key attributes depend on the whole of the key and not a subset of a compound key. The 1NF example can […]

Normalization, 1NF

I want to normalize a database schema for efficient transactions. I want to make sure all tables in the schema are in First Normal Form (1NF). There is no universal definition of 1NF; some definitions require only atomicity such that it may be impossible to violate 1NF if using a relational database such as MySQL […]

tail -F

I would like to follow an error log as errors are written. The GNU tail command support a –follow option that outputs data as a file grows. There’s also a –retry option that will keep trying to access a file even if it becomes inaccessible (e.g., during log rotation). The -F option is equivalent to […]

nohup &

I would like to run a script in the background and to keep running even after I log out. To run a command or script in the background use an ampersand, e.g., # ./long-running-script.sh & This is equivalent to suspending the process with Ctrl-Z, and then issuing bg, # ./long-running-script.sh ^Z [1]+ Stopped ./long-running-script.sh # […]

php echo and print

Is echo really, even negligibly, faster than print in php? Conventional wisdom of the Internet says “yes”, because print performs one extra op (it returns a value). I’ve seen performance charts with 20% or more difference in performance. I tested this and for the life of me could not find any significant difference. This is […]