r/DatabaseHelp • • Jul 19 '17

Calculating Field Completeness (MySQL, probably)

About 3 months ago, I posted in /r/databse about some of the stuff I had gone through to do this, and asked for advice.

The report is quarterly, so I'm back, but in a more appropriate sub.

I have a single table (212 columns by just under a million rows) and I need to find, for each column, the number of filled vs empty values.

This time around I've settled on PHP and MySQL to do the work. I have a query that gets the data I need, but it's really slow.

(Note: This is for the column "address_1")

SELECT
	`address_1` AS `Field`,
	((Y.`rows` /(X.`rows` + Y.`rows`))*100) AS `%Filled`,
	((X.`rows` /(X.`rows` + Y.`rows`))*100) AS `%Empty`,
	Y.`rows` AS `Filled`,
	X.`rows` AS `Empty`,
	'ALL' AS `Market`
FROM
	(
	SELECT
		"(Blank)" AS `content`,
		COUNT(*) AS `rows`
	FROM `2017-07-15_21:04`
	WHERE `address_1` = ""	) X,
	(
	SELECT
		"Filled" AS `content`,
		COUNT(*) AS `rows`
	FROM `2017-07-15_21:04`
	WHERE `address_1` != "") Y;

I found a few code snippets that show how to do this as a stored function, and I have that set up. This means that, in theory, I can run one command and get all 212 rows of results. In practice, however, I have to kill the process because it never finishes, but maxes my CPU and RAM after the timeout goes by.

The code for the Stored procedure:

DELIMITER //
CREATE PROCEDURE cfc()
BEGIN
	DECLARE Num_rows INT; 
	DECLARE I INT; 
	DECLARE Col_name VARCHAR(50); 
	DECLARE Done INT DEFAULT FALSE; 
	DECLARE Col_names CURSOR FOR
		SELECT COLUMN_NAME
		FROM Information_schema.Columns
		WHERE TABLE_NAME = '2017-07-15_21:04'
		ORDER BY Ordinal_position;
	
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET Done = TRUE;

	SET I = 1; 
	OPEN Col_names; 
	
	The_loop: WHILE I < 116 DO FETCH Col_names INTO Col_name;
		INSERT INTO `cfc_output`(`Field`, `PFilled`, `PEmpty`, `#Filled`, `#Empty`, `Market`)
		SELECT
			Col_name AS `Field`,
			((Y.`rows` /(X.`rows` + Y.`rows`))*100) AS `%Filled`,
			((X.`rows` /(X.`rows` + Y.`rows`))*100) AS `%Empty`,
			Y.`rows` AS `Filled`,
			X.`rows` AS `Empty`,
			'ALL' AS `Market`
		FROM
			(
			SELECT
				"(Blank)" AS `content`,
				COUNT(*) AS `rows`
			FROM `2017-07-15_21:04`
			WHERE Col_name = ""	) X,
			(
			SELECT
				"Filled" AS `content`,
				COUNT(*) AS `rows`
			FROM `2017-07-15_21:04`
			WHERE Col_name != "") Y;
		
		SET I = I + 1;
	END WHILE The_loop;
END; //
DELIMITER ;

So, my questions:

  1. How can I optimize this?
  2. Is there a better way to do this?
  3. What is this report called?

Edit: I put my code in :)

1 Upvotes

1 comment sorted by

1

u/farfromunique Jul 20 '17

Note to future-me (and anyone else who needs it): the project "common_schema" (Google Code) provides a 'ForEach" function to make this easier. Also, it's faster to use HeidiSQL instead of PHPMyAdmin.