2012-03-27 14 views
5

¡Estoy "bien" en MySQL básico, pero esto es "MANERA SOBRE MI CABEZA"!Cómo importar la base de datos, actualizar productos que han cambiado, eliminar productos que se han eliminado

Objetivos:

  • base de datos de importación
  • actualizar los productos que han cambiado
  • productos de eliminación que se han eliminado
  • rápida y eficiente

la tabla (s) base de datos se ENORME, la velocidad es un problema.

No tiene que MyISAM es inoDB sería más rápido? Cada base de datos estará en una tabla única.

se me dio esto como un punto de partida para lo que estoy tratando de hacer:

CREATE TABLE `table` LIKE LiveTable 
LOAD DATA INFILE..... INTO `table` 
UPDATE `table` SET delete=1; -- Set the delete field to true because it will not have been updated 
UPDATE `table` INNER JOIN`table`ON `LiveTable.ID`=`table.ID` 
SET LiveTable.Col1=table.Col1, LiveTable.Col2=table.Col2….. delete=0 
INSERT INTO LiveTable(ID,Col1,Col2,… delete=0) 
SELECT ID,Col1,Col2,...FROM `table` 
LEFT JOIN LiveTable 
ON table.ID = LiveTable.ID 
WHERE LiveTable.ID IS NULL 
DELETE FROM LiveTableWHERE delete = 0 
EMPTY TABLE `table` 

> CREATE TABLE `product_table` (
>  `programname` VARCHAR(100) NOT NULL, 
>  `name`  VARCHAR(160) NOT NULL, 
>  `keywords` VARCHAR(300) NOT NULL, 
>  `description` TEXT NOT NULL, 
>  `sku`   VARCHAR(100) NOT NULL, 
>  -- This is the only "unique identifier given, none will be duplicates" 
>  `price`  DECIMAL(10, 2) NOT NULL, 
>  `created`  TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 
>  `updatedat` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00', 
>  `delete`  TINYINT(4) NOT NULL DEFAULT '0', 
>  PRIMARY KEY (`sku`) ) ENGINE=myisam DEFAULT CHARSET=latin1; 
> 
> CREATE TABLE IF NOT EXISTS `temptable` LIKE `product_table`; 
> 
> TRUNCATE TABLE `temptable`; -- Remove data from temp table if for some 
> reason it has data in it. 
> 
> LOAD DATA LOW_PRIORITY LOCAL INFILE "catalog.csv" INTO TABLE 
> `temptable` FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" 
> LINES TERMINATED BY "\n" IGNORE 1 LINES (`PROGRAMNAME`, `NAME`, 
> `KEYWORDS`, `DESCRIPTION`, `SKU`, `PRICE`); 
> 
> 
> UPDATE `temptable` SET `delete` = 1; -- Set the delete field to 
> true UPDATE `temptable` ttable 
>  INNER JOIN `product_table` mtable 
>   ON (mtable.sku = ttable.sku) SET mtable.programname = ttable.programname, 
>  mtable.name = ttable.name, 
>  mtable.keywords = ttable.keywords, 
>  mtable.description = ttable.description, 
>  mtable.sku = ttable.sku, 
>  mtable.price = ttable.price, 
>  mtable.created = ttable.created, 
>  mtable.updatedat = NOW(),-- Set Last Update 
>  mtable.delete = 0; -- Set Delete to NO 
> 
> -- Not sure what this is for... I'm LOST at this part... 
> INSERT INTO `product_table` VALUES  (`programname`, 
>    `name`, 
>    `keywords`, 
>    `description`, 
>    `sku`, 
>    `price`, 
>    `created`, 
>    `updatedat`, 
>    `delete`); 
> 
> -- This type of join requires alias as far as I know? 
> SELECT `programname`, 
>  `name`, 
>  `keywords`, 
>  `description`, 
>  `sku`, 
>  `price`, 
>  `created`, 
>  `updatedat`, 
>  `delete` FROM `temptable` tmptable 
>  LEFT JOIN `product_table` maintbl 
>   ON tmptable.sku = maintbl.sku WHERE maintbl.sku IS NULL; 
> 
> DELETE FROM `product_table` WHERE `delete` = 0; 
> 
> TRUNCATE TABLE `temptable`; `` remove all the data from temporary 
> table. 

Respuesta

4

que respondieron a esta pregunta a mí mismo aquí: https://dba.stackexchange.com/questions/16197/innodb-update-slow-need-a-better-option/16283#16283

Utilizando la información que he recibido de aquí, la web y varias salas de chat de Internet, yo he llegado con. fuente web: http://www.softwareprojects.com/resources/programming/t-how-to-use-mysql-fast-load-data-for-updates-1753.html

[DEMO][1] http://sqlfiddle.com/#!2/4ebe0/1 

El proceso es:

Import into a new temp table. 
Update The old table information with information in Temp table. 
Insert new data into the table. (Real world I'm making a new CSV file and using LOAD INTO for the insert) 
delete everything that is no longer in the data feed. 
delete the temp table. 

Esto parece los procesos más rápidos hasta ahora.

Déjame saber cuál es tu opinión.

2

InnoDB es por lo general mucho mejor que en las mesas de MyISAM estar disponible mientras INSERT, y UPDATEDELETE están sucediendo, porque InnoDB usa el bloqueo de nivel de fila para actualizaciones, mientras que MyISAM usa bloqueo de nivel de tabla.

Ese es el primer paso.

El segundo paso es deshabilitar todos los índices en la tabla antes de cargar datos en una tabla usando ALTER TABLE .. DISABLE KEYS y luego habilitarlos de nuevo después de la carga usando ALTER TABLE .. ENABLE KEYS.

Los dos anteriores muestran grandes mejoras en su rendimiento.

Como otra optimización, al realizar actualizaciones a gran escala, divídalas en lotes (quizás en función de la clave principal) para que todas las filas no se bloqueen simultáneamente.

+0

Gracias por la información InnoDB lo es. No sé si usted lo sabe, pero la porción "> INSERTAR EN 'VALORES_categoría_producto" no me afecta, creo que falta algo allí ... – Brad

+0

no responde a la totalidad pregunta .... Sin embargo, no brinda información útil. – Brad

Cuestiones relacionadas