我正忙着为一个关于度假屋的网站实施 Solr。该网站使用 Postgres 作为主数据库。对于搜索结果,我们希望使用 Solr 作为后端来获取可用的度假屋。
通过使用带有 JdbcDataSource 的 DataImportHandler 来导入部分数据库。
DataImportHandler 的缩短版本:
<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
<dataSource type="JdbcDataSource" driver="org.postgresql.Driver" name="Solr"
url="jdbc:postgresql://host:port/database" user="*" password="*" readOnly="true"/>
<document>
<entity name="availabilities" transformer="RegexTransformer" pk="id"
query="
SELECT concat('A',pa.availability_id,'-',pad.start_date,'-',pad.period_type_id) as unique_availability_id,
pa.property_id,
NULLIF(CONCAT(ST_X(pl.position),',',ST_Y(pl.position)),',') as locationhash,
pl.position_accurate,
true as is_availability,
region.child_id as city_id,
region.ancestor_id as province_id,
(
SELECT array_to_string(array(SELECT binnen.ancestor_id
FROM fewo_Location_Ancestry binnen
WHERE binnen.child_id = region.child_id
AND binnen.ancestor_type_id = 12), ',')
) AS region_id,
pl.country_id,
pl.min_persons,
pl.max_persons,
fap.bedrooms,
pl.specifications,
pl.property_state_id,
pa.availability_id,
pad.period_type_id,
pad.start_date,
pad.end_date,
(
SELECT COUNT(*) > 0 FROM fewo_last_minute_details flmd
WHERE flmd.property_id = pa.property_id
AND flmd.details_id = pad.details_id
LIMIT 1
) AS last_minute,
CASE (
SELECT COUNT(*) > 0 FROM fewo_last_minute_details flmd
WHERE flmd.property_id = pa.property_id
AND flmd.details_id = pad.details_id
LIMIT 1
) WHEN true THEN pad.discount_price
ELSE pad.price
END as price,
pl.positioning_fee,
pl.sort_order
FROM fewo_property_availability_details pad
INNER JOIN fewo_property_availability pa USING (availability_id)
INNER JOIN fewo_Property_Location pl ON pa.property_id=pl.property_id
INNER JOIN fewo_all_properties fap ON pl.property_id=fap.property_id
INNER JOIN fewo_Location_Ancestry region ON (region.child_id =pl.location_id AND region.ancestor_type_id = 7)
WHERE pad.start_date > current_date
">
<field name="id" column="unique_availability_id"/>
<field name="property_id" column="property_id"/>
<field name="parent_id" column="property_id"/>
<field name="is_availability" column="is_availability"/>
<field name="positionCoord" column="locationhash"/>
<field name="position_accurate" column="position_accurate"/>
<field name="city_id" column="city_id"/>
<field name="province_id" column="province_id"/>
<field name="region_id" column="region_id" splitBy="," sourceColName="region_id"/>
<field name="country_id" column="country_id"/>
<field name="min_persons" column="min_persons"/>
<field name="max_persons" column="max_persons"/>
<field name="bedrooms" column="bedrooms"/>
<entity name="fewo_all_property_specifications" transformer="foo.SpecTransformer" pk="property_id"
cacheKey="property_id"
cacheLookup="availabilities.property_id"
query="SELECT property_id, specification_id, COALESCE(value,'true') as val FROM fewo_all_property_specifications"
processor="CachedSqlEntityProcessor">
</entity>
<field name="property_state_id" column="property_state_id"/>
<field name="availability_id" column="availability_id"/>
<field name="period_type_id" column="period_type_id"/>
<field name="start_date" column="start_date"/>
<field name="end_date" column="end_date"/>
<field name="last_minute" column="last_minute" />
<field name="price" column="price"/>
<field name="positioning_fee" column="positioning_fee"/>
<field name="sort_order" column="sort_order"/>
</entity>
</document>
</dataConfig>
导入运行大约一个小时,将 1300 万条记录导入 Solr。问题是在导入期间无法更新表 fewo_property_availability_details 因为有一个 AccessShareLock 锁定了表。这可以防止将数据更新/插入到表中,并且这些查询会排队。一段时间后,它们堆积太多,数据库出现故障。
我的问题是:有没有一种很好的方法来导入数据而不会过多地阻碍常规查询?就像在 x 条导入记录后开始新事务一样,也给其他查询时间运行?
我正在使用在 Ubuntu 12.04 上运行的 Solr 4.0 和 Postgres 9.1。
谢谢