I actually came across
this "bug" that had me a little spooked on tinkering with the AI column.
I'm looking at this relations table, the left column is a VARCHAR(64), right and order columns are INT columns. Realistically, I can probably chop that left column to 24.
I think in my worst case scenario an INSERT-SELECT to generate fresh order_id values would need 30MB of memory to store the SELECT results. And that's if there were 1M+ rows. I'd have a tough time realistically seeing more than 100K rows in this operation, which would need closer to 3MB. This is a back-end operation, it's not going to be something that happens often.
When I consider things like a checkbox that has grandparent categories adopt the items of deleted categories automatically, just dropping the existing rows and inserting fresh rows starts to seem like a better idea as too as I'll be gathering the cat_ids in chunks.
Here's the three queries I'm looking at, pre-grandparent-adoption.
$c->categories = default_for_in(array(), 'categories', $_POST);
$c->new_category = default_for_in('', 'new_category', $_POST, imasks::category_id);
if(default_for_in(false, 'submit', $_POST))
{
$c->sql_in = '';
foreach($c->categories as &$c->category)
{
if(preg_match(imasks::category_id, $c->category))
{
$c->sql_in .= ",'{$c->category}'";
}
}
if($c->sql_in)
{
$c->sql_in = substr($c->sql_in, 1);
if($c->new_category)
{
$c->sql = '
INSERT IGNORE INTO ' . CAT_WP_RELATIONS_TABLE . ' (cat_id, item_id)
SELECT "' . $db->real_escape_string($c->new_category) . '", item_id
FROM ' . CAT_WP_RELATIONS_TABLE . "
WHERE cat_id IN({$c->sql_in})";
if($db->query($c->sql))
{
$page->messages .= new message('Added New Category/Item Relations', 'success');
}
else
{
$page->messages .= new message($db->error, 'error');
}
}
$c->sql = 'DELETE FROM ' . CAT_WP_RELATIONS_TABLE . " WHERE cat_id IN({$c->sql_in})";
if($db->query($c->sql))
{
$page->messages .= new message('Deleted Old Category/Item Relations', 'success');
$c->sql = 'DELETE FROM ' . CATEGORIES_TABLE . " WHERE id IN({$c->sql_in})";
if($db->query($c->sql))
{
$page->messages .= new message('Deleted Categories', 'success');
}
else
{
$page->messages .= new message($db->error, 'error');
}
}
else
{
$page->messages .= new message($db->error, 'error');
}
unset($c->sql, $c->sql_in);
}
else
{
$page->messages .= new message('No Categories Selected');
}
}
- $c->categories = default_for_in(array(), 'categories', $_POST);
- $c->new_category = default_for_in('', 'new_category', $_POST, imasks::category_id);
-
- if(default_for_in(false, 'submit', $_POST))
- {
- $c->sql_in = '';
- foreach($c->categories as &$c->category)
- {
- if(preg_match(imasks::category_id, $c->category))
- {
- $c->sql_in .= ",'{$c->category}'";
- }
- }
- if($c->sql_in)
- {
- $c->sql_in = substr($c->sql_in, 1);
- if($c->new_category)
- {
- $c->sql = '
- INSERT IGNORE INTO ' . CAT_WP_RELATIONS_TABLE . ' (cat_id, item_id)
- SELECT "' . $db->real_escape_string($c->new_category) . '", item_id
- FROM ' . CAT_WP_RELATIONS_TABLE . "
- WHERE cat_id IN({$c->sql_in})";
- if($db->query($c->sql))
- {
- $page->messages .= new message('Added New Category/Item Relations', 'success');
- }
- else
- {
- $page->messages .= new message($db->error, 'error');
- }
- }
-
- $c->sql = 'DELETE FROM ' . CAT_WP_RELATIONS_TABLE . " WHERE cat_id IN({$c->sql_in})";
- if($db->query($c->sql))
- {
- $page->messages .= new message('Deleted Old Category/Item Relations', 'success');
- $c->sql = 'DELETE FROM ' . CATEGORIES_TABLE . " WHERE id IN({$c->sql_in})";
- if($db->query($c->sql))
- {
- $page->messages .= new message('Deleted Categories', 'success');
- }
- else
- {
- $page->messages .= new message($db->error, 'error');
- }
- }
- else
- {
- $page->messages .= new message($db->error, 'error');
- }
- unset($c->sql, $c->sql_in);
- }
- else
- {
- $page->messages .= new message('No Categories Selected');
- }
- }
Strong with this one, the sudo is.