MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for...

41
MAGENTO 2.0 INDEXATION TECHNIQUES FOR HIGH PERFORMANCE by Ivan Chepurnyi

Transcript of MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for...

Page 1: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

MAGENTO 2.0INDEXATION TECHNIQUES FOR HIGH

PERFORMANCE

by Ivan Chepurnyi

Page 2: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

WHAT IS A MAGENTO INDEX?

Page 3: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

MAGENTO INDEXRepresented as a separate tableOptimized for retrieval, filtration and/or sorting.Just a snapshot of data at some point in time.

Page 4: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

WHEN TO CREATE IT?

Page 5: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

DATA FILTRATIONProduct VisibilityProduct StatusCategory Relation

Page 6: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

DATA SORTINGCategory PositionBestsellersStock Quanttities

Page 7: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

COMPLEX LOGIC CACHEStock AvailabilityProduct PriceAnchor Category Relation

Page 8: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

WHEN DO NOT TO USE THEM?

Page 9: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

FOR DATA RETRIEVALIt does not make sense to flatten EAV structure for retrieval

Page 10: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

WHEN ACCURATE DATA IS CRITICALUsing price or stock index in shopping cart is a bad idea

Page 11: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

HIGH AVAILABILITY INDEXATIONCHALLANGES

Page 12: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

TABLE LOCKSIndex generation can create a lock on table you don't expect

UDPATE some_index_table index INNER JOIN catalog_product_entity_varchar product_data ON ... SET index.some_value = product_data.value

Page 13: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

SOLUTIONAvoid cross table updates on live tablesCreate snapshot of data for your index

Page 14: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

MYSQL MEMORYMySQL memory consuption is increasing with amount of

records you need to update.

These are known MySQL serial killers:INSERT INTO .. SELECT ... FROM

UPDATE ... JOIN ... SET ...

Page 15: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

SOLUTIONRange your updates into smaller data-sets

Page 16: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

INDEX DOWNTIMEDuring re-indexation there is a step when index table is

locked and no data is available in it.

Page 17: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

SOLUTIONCreate two identical index tablesRe-index only one at a timeSwitch one to use on the frontend via configuration model

Page 18: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

DID ANYONE CREATED AN INDEXER INMAGENTO 1.X?

It was a nightmare comparing to Magento 2.0...

Page 19: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

IT IS NOW INCREDIBLY SIMPLE...

Page 20: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

ONLY TWO CONFIGURATION FILESREQUIRED, AND NO EVENT MATCHING MAGIC:

indexer.xml and mview.xml

Page 21: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

MVIEW.XMLAllows you to subscribe to changes in any table

Magento\Framework\Mview\ActionInterface<view id="mview_id" class="MviewClass" group="indexer"> <subscriptions> <table name="table_to_check" entity_column="primary_key" /> </subscriptions> </view>

Page 22: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

WHY IS IT CALLED MVIEW?

Page 23: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

INDEXER.XMLJust a regular Magento indexer definition

Magento\Framework\Indexer\ActionInterface<indexer id="indexer_code" view_id="mview_code" class="YourIndexerClass"> <title translate="true">Your Title</title> <description translate="true">Your Description</description> </indexer>

Page 24: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

So how to implement HA index in Magento 2.0?

Page 25: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

I really like re-usable components

So I come up with such indexer structure

Page 26: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

Index Population

List Generator

Data Generator

Index Processor

Index Sp

ecific Classes

Public API

Indexer

MView Action

Indexer Action

Service Classes

Table Builder

Index Metadata

Generic Classes

Page 27: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

INDEXERGeneric interface for usage in MView and Indexer ActionHas knowledge about the index structureCreates index conditions for processor

Page 28: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

SAMPLE INTERFACEinterface IndexerInterface public function executeFull();

public function executeByIds(array $ids);

Page 29: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

LIST GENERATORCreates fundation for our indexerConsists mostly from entity identifiers

Page 30: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

SAMPLE INTERFACEuse Magento\Framework\Db\Ddl\Table;

interface ListGeneratorInterface /** * @return Table */ public function generate(ConditionInterface $condition);

Page 31: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

INDEX CONDITIONDecouples creation and applying of condition

Page 32: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

SAMPLE INTERFACEuse Magento\Framework\DB\Select;

interface ConditionInterface public function apply( Select $select, $alias, array $columnMap = [] );

Page 33: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

DATA GENERATORCreates final subset of index dataData is based on List Generator data

Page 34: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

SAMPLE INTERFACEinterface DataGeneratorInterface /** * @return Table */ public function generate(Table $listTable);

Page 35: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

INDEX PROCESSORRecieves list of index conditions from indexerDecides how to populate an index tableToggles index table via metadata instance

Page 36: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

SAMPLE INTERFACEinterface ProcessorInterface public function process($conditions, $live = false);

Page 37: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

INDEX METADATAControls which table to use for indexer and which forfrontendShould be used in your entity repository to filter data by itProvides usefull attribute information

Page 38: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

SAMPLE INTERFACEinterface MetadataInterface public function getTableToIndex($live = false);

public function getIndexTable();

public function toggleTable();

public function getAttributesInfo($entityType, $attributeCodes);

Page 39: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

REAL INDEX EXAMPLECreated in 3 hours during the workshop on the first

day#DevParadise

http://bit.ly/m2-boilerplate

Page 40: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

WANT TO LEARN MORE ABOUTTHESE TECHNIQUES?

Catch me at coffee break or during a er party!

Page 41: MAGENTO 2 - createhosting.co.nz...MAGENTO INDEX Represented as a separate table Optimized for retrieval, filtration and/or sorting. Just a snapshot of data at some point in time. WHEN

Q&A@IvanChepurnyi

[email protected]